【问题标题】:Error while updating data in Laravel在 Laravel 中更新数据时出错
【发布时间】:2017-06-16 11:59:27
【问题描述】:

无法更新数据,

控制器代码

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();
        });

谁能告诉我,我做错了什么

【问题讨论】:

    标签: laravel laravel-5 laravel-5.4


    【解决方案1】:

    在填充模型时,您需要使用 $request->only(['userType, 'firstNAme', 'lastName']) 等等等等。不能传入 _method 和 _token 之类的索引,否则它会尝试将它们保存到数据库中并且不知道如何处理它们。

    【讨论】:

    • 您的 User 模型是否设置了 $fillable 或 $guarded 属性?如果是这样,他们需要与您传递给 update() 方法的内容保持一致。如果愿意,请发布您的 Use 模型类。
    • 我一直受保护 $fillable = [ 'userType', 'firstName', 'lastName', 'gender','personalContact','officeContact','email','country','state ','城市','地址','邮编'];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2011-12-30
    • 1970-01-01
    • 2021-07-13
    • 2018-06-30
    • 1970-01-01
    相关资源
    最近更新 更多