laravel类型错误:传递给Illuminate \ Database \ Eloquent \ Model :: save()的参数1必须为数组类型,给定对象 [英] laravel Type error: Argument 1 passed to Illuminate\Database\Eloquent\Model::save() must be of the type array, object given
问题描述
我有一个实体,每个实体都有一个地址.
I have an entity and each entity has an address.
我有2个具有如下关系的表:
I have 2 tables with relationship such as:
实体:
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中调用.php行146
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
我该如何解决这个问题?
How can I solve this issue?
推荐答案
我认为您只需要使用不带任何参数的save()
方法.我已经在php artisan tinker
中以相同的结构进行了尝试.在遇到相同的错误后,我尝试删除save()
方法中的$entity
参数:
I think you just need to use save()
method without any parameters. I have try it in php artisan tinker
with same structure. And after I get same error, I try to remove the $entity
parameter inside save()
method:
// After update, you should only use save() not save($entity)
$entity->address->save();
我希望我能为您的问题提供正确答案:-D
I hope I give correct answer for your problem :-D
这篇关于laravel类型错误:传递给Illuminate \ Database \ Eloquent \ Model :: save()的参数1必须为数组类型,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!