laravel 传递给 Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() 的参数 1 必须是数组类型,给定的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44086939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:00:28  来源:igfitidea点击:

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() must be of the type array, object given

phplaravelmodel-view-controller

提问by Agus Suparman

I want to update a data in the database i have controller

我想更新我有控制器的数据库中的数据

public function update(Identity $identity, Request $request)
{
  $data = new Identity();
  $data->date  = $request['date'];
  $data->code   = $request['code'];
  $data->name   = $request['name'];
  $request->user()->identity()->update($data);
  Session::flash('flash_message', 'Update success.');
  return redirect('identity.index');
}

Model Identity

模特身份

public function user()
{
// Each data is owned by only one user
return $this->belongsTo('App\User');
}

Model User

模型用户

public function identity()
{
// Each user will have a lot of data
return $this->hasMany('App\Identity');
}

And i found an error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() must be of the type array, object given.

我发现了一个错误:传递给 Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() 的参数 1 必须是给定的对象类型数组。

回答by Sandeesh

You already have the Identity model with the route model binding. You can do one of the below.

您已经拥有带有路由模型绑定的 Identity 模型。您可以执行以下操作之一。

public function update(Identity $identity, Request $request)
{
    $identity->date = $request['date'];
    $identity->code = $request['code'];
    $identity->name = $request['name'];
    $identity->save();

    Session::flash('flash_message', 'Update success.');
    return redirect('identity.index');
}

Or (Make sure you set the $fillable property in the model for this to work)

或者(确保您在模型中设置了 $fillable 属性以使其正常工作)

public function update(Identity $identity, Request $request)
{
    $identity->update([
        'date' => $request['date'],
        'code' => $request['code'],
        'name' => $request['name'],
    ]);

    Session::flash('flash_message', 'Update success.');
    return redirect('identity.index');
}

回答by Matt S

This line

这条线

$data = new Identity();

creates an object. Below that you are setting its properties. Instead, it looks like you can pass your properties directly into the function:

创建一个对象。在此之下,您正在设置其属性。相反,您似乎可以将属性直接传递给函数:

public function update(Identity $identity, Request $request)
{
  $request->user()->identity()->update(array($request));
  ...
}

Of course you might also want to restrict your request to just what's needed:

当然,您可能还想将您的请求限制在需要的范围内:

public function update(Identity $identity, Request $request)
{
  $params = array_intersect_key(array($request), array_flip(['date', 'code', 'name']))
  $request->user()->identity()->update($params);
  ...
}