2016-06-07 230 views
0

我几天前使用Laravel 5.2,并且遇到了问题! 我希望有人能帮助我。Laravel 5.2更新数据

我想要一个特殊的程序,所以我不使用资源。 我的问题是,用户的数据没有在数据库中更新,我尝试了很多方法,但没有。

这里是我的控制器:

protected function edit($name) 
    { 
     return view('user.edit', array('user' => User::findByUsernameOrFail($name))); 
    } 

    protected function update($name, ProfileDataRequest $request) 
    { 
     $profile = User::findorfail($name); 

     $input = $request -> all(); 

     $profile->update($input); 

     return redirect()->action('[email protected]'); 
    } 

我的形式:

{!! Form::model($user, ['method' => 'PATCH', 'action' => ['[email protected]', $user -> name ]]) !!} 

     {{ csrf_field() }} 

     {!! Form::label('sms', 'SMS: ') !!} 
     {!! Form::checkbox('sms', 1, false) !!} 

     {!! Form::label('name', 'Name: ') !!} 
     {!! Form::text('name') !!} 



     {!! Form::submit('Submit') !!} 


{!! Form::close() !!} 

我的路线:

Route::get('/user', '[email protected]'); 
Route::get('/user/{name}', '[email protected]'); 
Route::get('/user/{name}/edit', '[email protected]'); 
Route::patch('/user/{name}', '[email protected]'); 

我的请求文件:

class ProfileDataRequest extends Request 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      'name' => 'required', 
      'sms' => 'required', 
     ]; 
    } 
} 

如果您有更简单的方法,请写信给我!

+1

有什么错误?应用程序是否进入更新功能? – Laerte

+0

如果我点击提交按钮,只刷新页面,并没有。 – Brhama

回答

1

您的控制器方法是protected它们应该是public以便可访问。

更换protectedpublic

public function update(Request $request) { 
    //your code here 
} 

Laravel Http Controller

+0

非常感谢! – Brhama