2017-06-15 77 views
0

無法更新數據,錯誤而Laravel

控制器代碼更新數據

public function update(Request $request, $id) 
    { 
     // 

     echo $request->all(); 
     /* $user= User::find($id); 
     $user->update($request->all()); 
     $user->save(); 
     return redirect('user');*/ 
    } 

數據不會得到更新,這就是爲什麼使用的回聲。

當使用

echo $request->all();  (line 86) 

得到了錯誤

(1/1) ErrorException 
    Array to string conversion 
    in userController.php (line 86) 

使用

dd($request->all()); 

了導致

array:14 [▼ 
    "_method" => "PUT" 
    "_token" => "zDkzEpJCKscUgAGvgFQwu6gKbtRwm8N8MdBHC9em" 
    "userType" => "Admin" 
    "firstName" => "Sda" 
    "lastName" => "ad" 
    "gender" => "M" 
    "personalContact" => "123" 
    "officeContact" => "132" 
    "email" => "ads" 
    "country" => "ds" 
    "state" => "ds" 
    "city" => "ddsf" 
    "address" => "dsf" 
    "zipCode" => "1234" 
] 

我的模型

class User extends Model 
{ 
    // 
    protected $fillable = [ 
     'userType', 'firstName', 'lastName', 'gender','personalContact','officeContact','email','country','state','city','address','zipCode' 
    ]; 
/* public $incrementing = false;*/ 
} 

用戶表遷移

Schema::create('users', function (Blueprint $table) { 
      $table->increments('Id'); 
      $table->uuid('userType'); 
      $table->string('firstName'); 
      $table->string('lastName'); 
      $table->char('gender'); 
      $table->string('personalContact'); 
      $table->string('officeContact'); 
      $table->string('email'); 
      $table->uuid('country'); 
      $table->uuid('state'); 
      $table->uuid('city'); 
      $table->string('address'); 
      $table->integer('zipCode'); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 

任何人都可以請讓我知道,我做了什麼錯

回答

1

您需要使用$請求 - >只(['用戶類型, 'firstNAme','lastName'])等等等填充你的模型。你不能傳入像_method和_token這樣的索引,否則它會嘗試將這些索引保存到數據庫中,而不知道如何處理它們。

+0

但存儲方法使用相同&其工作正常 – Sumeet

+0

您的用戶模型是否具有$ fillable或$ guarded屬性集?如果是這樣,他們需要與您傳遞給update()方法的內容對齊。如果您願意,請發佈您的使用模型類。 – btl

+0

我一直在保護$ fillable ='userType','firstName','lastName','gender','personalContact','officeContact','email','country','state','city', 'address','zipCode' ]; – Sumeet