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

时间:2017-07-12 14:01:06

标签: laravel laravel-5 eloquent

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

我有2个关系表,如:

实体:

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

use Searchable;

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

地址:

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

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

和我的控制员:

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

错误信息是:

  

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为你只需要使用save()方法而不需要任何参数。我在php artisan tinker中尝试了相同的结构。在我收到同样的错误后,我尝试删除$entity方法中的save()参数:

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

我希望我能为你的问题给出正确答案:-D