laravel 类型错误:传递给 Illuminate\Database\Eloquent\Model::save() 的参数 1 必须是数组类型,给定对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45059864/
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:17:36  来源:igfitidea点击:

laravel Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::save() must be of the type array, object given

laravellaravel-5eloquent

提问by Przemek Wojtas

I have an entity and each entity has an address.

我有一个实体,每个实体都有一个地址。

I have 2 tables with relationship such as:

我有 2 个表的关系,例如:

Entity:

实体:

protected $table = 'entities';
public $timestamps = true;

use Searchable;

public function address()
{
    return $this->hasOne('App\Address', 'entity_id');
}

Address:

地址:

protected $table = 'address';
public $timestamps = true;

public function entity()
{
    return $this->belongsTo('App\Entity', 'id');
}

and my controller:

和我的控制器:

 public function update(EntityRequestUpdate $request)
    {
        $id = $request->input('entity_id');
        $entity = Entity::with('address')
            ->find($id);
        $entity->name = $request->input('name');
        $entity->type = $request->input('type');
        $entity->email = $request->input('email');
        $entity->twitter_entity = $request->input('twitter');
        $entity->facebook_entity = $request->input('facebook');
        $entity->instagram_entity = $request->input('instagram');
        $entity->google_places = $request->input('google');
        $entity->address->building_name = $request->input('address1');
        $entity->address->street = $request->input('address2');
        $entity->address->town = $request->input('town');
        $entity->address->city = $request->input('city');
        $entity->address->postcode = $request->input('postcode');
        $entity->address->telephone = $request->input('telephone');
        $entity->address->save($entity);
        $entity->save();

        $result = $entity->save();

        if($result){
            $message = 'success';
        }else{
            $message = 'error';
        }
        return redirect()->back()->withInput()->with('message', $message);
    }

An error message is:

一条错误消息是:

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::save() must be of the type array, object given, called in C:\xampp\htdocs\laravel\app\Http\Controllers\EntityController.php on line 146

类型错误:传递给 Illuminate\Database\Eloquent\Model::save() 的参数 1 必须是数组类型,给定的对象,在 C:\xampp\htdocs\laravel\app\Http\Controllers\EntityController.php 上调用第 146 行

How can I solve this issue?

我该如何解决这个问题?

回答by Arigi Wiratama

I think you just need to use save()method without any parameters. I have try it in php artisan tinkerwith same structure. And after I get same error, I try to remove the $entityparameter inside save()method:

我认为你只需要使用save()没有任何参数的方法。我已经尝试过php artisan tinker使用相同的结构。在我得到同样的错误后,我尝试删除方法中的$entity参数save()

// After update, you should only use save() not save($entity)
$entity->address->save(); 

I hope I give correct answer for your problem :-D

我希望我能正确回答你的问题:-D