2015-06-10 14 views
1

我正在嘗試編輯/更新我的用戶表中的配置文件信息。修補程序更新Laravel中的經過身份驗證的用戶

我的想法是認證用戶能夠在Users表中編輯他自己的配置文件。

在註冊過程中,您只需填寫一對特定項目(用戶名,姓名,姓氏,電子郵件,密碼),但我還在用戶表中添加了多個列(城市,國家,電話,Twitter,臉書)。

我有一個配置文件用戶頁面(route ='/ profile'),其中顯示了所有的信息。當然,這在註冊過程中並不需要所有列未填寫:

Profile Page

我也有一個編輯頁面,需要補充信息的列編輯:

Profile Edit Page

以下是此editprofile.blade.php代碼(在這裏我試圖發送一個補丁的方法):

... 

{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!} 
    <div class="form-group form-horizontal"> 
     <div class="form-group"> 
       {!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       <label class="align-left">{{ $user->username}}<label>  
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       <label class="align-left">{{ $user->email}}<label> 
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       <label class="align-left">{{ $user->name}} {{ $user->lastname}}<p> 
      </div> 
     </div> 

     <br /> 

     <div class="form-group"> 
       {!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       {!! Form::Text('city', null, ['class' => 'form-control']) !!} 
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       {!! Form::Text('country', null, ['class' => 'form-control']) !!} 
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       {!! Form::Text('phone', null, ['class' => 'form-control']) !!} 
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       {!! Form::Text('twitter', null, ['class' => 'form-control']) !!} 
      </div> 
     </div> 

     <div class="form-group"> 
       {!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!} 
      <div class="col-md-6"> 
       {!! Form::Text('facebook', null, ['class' => 'form-control']) !!} 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-6 col-md-offset-4"> 
       {!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!} 
      </div> 
     </div> 

     </div> 
    </div> 
{!! Form::close() !!} 
... 

我這是我的HTTP/routes.php文件:

# Profile 
Route::get('/profile', '[email protected]'); 
Route::get('/profile/edit', '[email protected]'); 


Route::bind('user', function($id) { 
$user = App\User::find($id)->first(); 
}); 
Route::patch('user/{user}', '[email protected]'); 

我有一個HTTP /採購/ UpdateUserRequest.php驗證請求:

<?php namespace App\Http\Requests; 

use App\Http\Requests\Request; 
use Illuminate\Foundation\Http\FormRequest; 

class UpdateUserRequest extends Request { 

    public function authorize() 
    { 
    return false; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      'city' => 'max:30', 
      'country' => 'max:30', 
      'phone' => 'max:30', 
      'twitter' => 'max:30', 
      'facebook' => 'max:30' 
     ]; 
    } 

} 

我的HTTP /控制器/ ProfileController.php與更新功用:

<?php namespace App\Http\Controllers; 

use Auth; 

use App\Http\Requests; 
use App\Http\Controllers\Controller; 

use Illuminate\Http\Request; 

class ProfileController extends Controller { 

    public function edit() 
    { 
     $user = Auth::user(); 

     return view('pages.editprofile')->withUser($user); 
    } 

    public function update(UpdateUserRequest $request, User $user) 
    { 
     $user->fill($request->all()); 
     $user->save(); 
     return redirect()->view('pages.editprofile')->withUser($user); 
    } 


} 

在它看來我甚至不能再打開我的'editprofile.blade.php頁面,除非我刪除米「路線」 &「梅索德」的那一刻y形式。 我不斷收到此錯誤:

Route/Methode Error

任何人都可以指導我應該怎麼恰好觸發修補梅索德?

回答

1

改變您的形式開始標記

{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!} 

您忘記 ' - > ID'

UPDATE

更改您路線 從

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

Route::patch('user/{user}', 'as' => 'profile.patch', '[email protected]'); 

和您的形式開始標記

{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!} 
+0

如果我嘗試,我得到這個錯誤: ErrorException在routes.php文件第23行: 試圖讓非對象的屬性 ==> 23-25行: 路線::綁定(「用戶/」 $ user-> id,function($ id){user = App \ User :: find($ id) - > first(); \t}); – Pex

+1

我已經更新了我的答案。嘗試一下。 –

+0

好吧我通過添加如下括號來解決了意想不到的問題:Route :: patch('user/{user}',['as'=>'profile.patch','uses'=>'ProfileController @更新']);儘管現在當我按下保存配置文件時,它只會轉到網址:Localhost/user/1並在網站上說'禁止'。 – Pex

0

你需要改變:

{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!} 

到:

{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!} 

在您的表單創建網址的時刻與User對象轉換爲Json - 這就是爲什麼它不起作用。

+0

如果我嘗試得到這個錯誤:route.php中的ErrorException第23行:嘗試獲取非對象的屬性==>第23-25行:Route :: bind('user /'。$ user-> id,函數($ id){$ user = App \ User :: find($ id) - > first();}); – Pex

相關問題