保存 belongsTo 关系
save a belongsTo relationship
我有一个经典的一对多关系,我正在尝试保存 belongsTo 端的模型。
这 2 个模型具有以下关系:
// Model myModel
function domicile()
{
return $this->belongsTo('App\Address', 'domicile_id');
}
// Model Address
function myModels()
{
return $this->hasMany('App\MyModel', 'domicile_id');
}
这就是我试图保存它的方法:
$myModel->domicile()->save($my_array);
使用这段代码我得到错误:
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::save()
如果我使用此代码(不带括号):
$myModel->domicile->save($my_array);
我没有收到任何错误,但模型没有保存。
我知道有方法 associate
,但我需要更新现有记录,而不是保存新记录。
因为 $myModel->domicile()->save($my_array);
与 $myModel->domicile->save($my_array);
完全不同:
$myModel->domicile()
会产生一个BelongsTo
对象,不支持save
因为save
是HasMany
的一个方法实例,而不是 BelongsTo
个实例,你应该使用 associate(YourModel)
$myModel->domicile
会产生一个关联元素的Model
对象,支持save(array)
方法,但是那个数组是options
数组, 正如 api 所说 https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Model.html#method_save
所以换句话说,如果你有一个(地址)对多(住所)的关系,如果你想关联一个或多个住所的地址,你必须使用save
或saveMany
(https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/HasMany.html#method_save), instead if you want to associate to a domicile a address, you should use associate
(https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/BelongsTo.html#method_associate)... 请记住,如果你想这样做,你应该用括号调用属性,以便返回一个 HasMany
对象或一个 BelongsTo
对象,而不是模型或集合(如果调用不带括号的属性,您将得到)
为了保存 belongsTo 关系,您必须使用 fill
函数,而不是使用 save
函数。
这样:
$myModel->domicile->fill($my_array);
$myModel->domicile->save();
您必须使用 associate()
+ save()
才能存储 BelongsTo 关系:
$myModel->domicile()->associate($domicile);
$myModel->save();
我有一个经典的一对多关系,我正在尝试保存 belongsTo 端的模型。
这 2 个模型具有以下关系:
// Model myModel
function domicile()
{
return $this->belongsTo('App\Address', 'domicile_id');
}
// Model Address
function myModels()
{
return $this->hasMany('App\MyModel', 'domicile_id');
}
这就是我试图保存它的方法:
$myModel->domicile()->save($my_array);
使用这段代码我得到错误:
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::save()
如果我使用此代码(不带括号):
$myModel->domicile->save($my_array);
我没有收到任何错误,但模型没有保存。
我知道有方法 associate
,但我需要更新现有记录,而不是保存新记录。
因为 $myModel->domicile()->save($my_array);
与 $myModel->domicile->save($my_array);
完全不同:
$myModel->domicile()
会产生一个BelongsTo
对象,不支持save
因为save
是HasMany
的一个方法实例,而不是BelongsTo
个实例,你应该使用associate(YourModel)
$myModel->domicile
会产生一个关联元素的Model
对象,支持save(array)
方法,但是那个数组是options
数组, 正如 api 所说 https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Model.html#method_save
所以换句话说,如果你有一个(地址)对多(住所)的关系,如果你想关联一个或多个住所的地址,你必须使用save
或saveMany
(https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/HasMany.html#method_save), instead if you want to associate to a domicile a address, you should use associate
(https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Relations/BelongsTo.html#method_associate)... 请记住,如果你想这样做,你应该用括号调用属性,以便返回一个 HasMany
对象或一个 BelongsTo
对象,而不是模型或集合(如果调用不带括号的属性,您将得到)
为了保存 belongsTo 关系,您必须使用 fill
函数,而不是使用 save
函数。
这样:
$myModel->domicile->fill($my_array);
$myModel->domicile->save();
您必须使用 associate()
+ save()
才能存储 BelongsTo 关系:
$myModel->domicile()->associate($domicile);
$myModel->save();