Полиморфные отношения
Last updated
Last updated
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->integer('imageable_id')->nullable();
$table->string('imageable_type')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
public function imageable()
{
return $this->morphTo();
}
public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
// выбираем пост с айди 1
$post = Post::find(1);
// создаём изображение
$image = new Image([
'url' => 'some image url'
]);
// связываем наше изображение с постом и сохраняем его
$image->imageable()->associate($post);
$image->save();
$post = Post::find(1);
$image = Image::find(1);
// отсоединяем imageable(post/user) от изображения и сохраняем его
$image->imageable()->dissociate($post);
$image->save();
// выбираем пост с айди 1
$post = Post::find(1);
// получаем его image
$image = $post->image;
// выбираем изображение с айди 1
$image = Image::find(1);
// получаем его imageble (post/user)
$imageable = $image->imageable;
// выбираем пост с айди 1
$post = Post::find(1);
// удаляем пренадлежащее ему изображение
$post->image()->delete();
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
comments
id - integer
body - text
commentable_id - integer
commentable_type - string
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string("body");
$table->integer('commentable_id')->nullable();
$table->string("commentable_type")->nullable();
$table->timestamps();
});
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function commentable()
{
return $this->morphTo();
}
Операции с данными идентичны обычным (не полиморфным) связям
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
Schema::create('taggables', function (Blueprint $table) {
$table->integer("tag_id");
$table->integer("taggable_id")->nullable();
$table->string("taggable_type")->nullable();
});
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("name");
$table->timestamps();
});
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
public function posts()
{
return $this->morphedByMany(Post::class, 'taggable');
}
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable');
}
Операции с данными идентичны обычным (не полиморфным) связям