2016-06-07 232 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