Answer the question
In order to leave comments, you need to log in
How to properly set up the relationship in this case?
I'm doing something like a page builder.
There is a page entity (page) to which modules can be added (Text, Image, Link, etc.).
Started the following entities:
//Страница
class Page extends Model
{
public function modules()
{
return $this->hasMany(Module::class);
}
}
//Модуль
class Module extends Model
{
protected $fillable = [
'page_id',
'type',
'order'
];
public function texts()
{
return $this->hasMany(Text::class);
}
public function links()
{
return $this->hasMany(Link::class);
}
public function images()
{
return $this->hasMany(Image::class);
}
}
//Модуль Текст
class Text extends Model
{
protected $fillable = [
'module_id',
'text'
];
}
//Модуль Ссылка
class Link extends Model
{
protected $fillable = [
'module_id',
'url',
'name',
'order'
];
}
//Модуль Картинка
class Image extends Model
{
protected $fillable = [
'module_id',
'url',
'name'
];
}
class Module extends Model
{
protected $fillable = [
'page_id',
'type',
'order'
];
public function items()
{
if ($this->type === 'text') {
return $this->hasMany(Text::class);
} else if ($this->type === 'link') {
return $this->hasMany(Link::class);
} else if ($this->type === 'image') {
return $this->hasMany(Image::class);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question