在Laravel中更新数据时出错

时间:2017-06-15 19:26:09

标签: laravel laravel-5 laravel-5.4

无法更新数据,

控制器代码

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

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

数据没有得到更新,这就是使用echo的原因。

使用时

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 个答案:

答案 0 :(得分:1)

填写模型时,您只需要使用$ request->(['userType,'firstNAme','lastName'])等等。你不能传递像_method和_token这样的索引,否则会尝试将这些索引保存到数据库中,而不知道如何处理它们。