【发布时间】:2020-04-16 18:57:03
【问题描述】:
我有一对一的多态,我想找出更新现有关系的最佳方法。
class Image extends Model
{
/**
* Get the owning imageable model.
*/
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
/**
* Get the post's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
class User extends Model
{
/**
* Get the user's image.
*/
public function image()
{
return $this->morphOne('App\Image', 'imageable');
}
}
关于 PostController 中的更新方法
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
$post->image()->delete();
$post->image()->save(new Image([
'url'=> $request->input('image_url),
]));
}
如何在不先删除的情况下更新图像关系?
谢谢
【问题讨论】:
-
a
Post是否总是已经有使用此方法的图像? -
是的,总是有图片。
标签: laravel polymorphism relationship one-to-one