Я постоянно выполняю эту задачу.
Я хотел бы найти универсальное решение.
У меня есть несколько таблиц. Они имеют набор идентичных полей.
Таблица type_applications
id name title
1 game Games
2 app Application
3 arcade Arcade
Таблица type_tasks
id name title
1 create Create App
2 update Update App
3 publish Publish App
Таблица status_applications
id name title
1 created Games
2 moderation Moderated application
3 publish Application published
СуществуетТаблица, к которой относятся эти каталоги.
Таблица задач
id name title status_id type_task_id type_application_id
1 first_app My First game 3 2 1
2 second_app My Second game 2 3 2
3 third_app My third game 1 1 3
Соответственно, существует 4 модели.
Модель задач связана с моделью типа приложения, причеммодель состояния и с моделью типа задачи.
public function type_application()
{
return $this->belongsTo(TypeApplication::class);
}
public function type_task()
{
return $this->belongsTo(TypeTask::class);
}
public function status()
{
return $this->belongsTo(Status::class);
}
Как сделать универсальную таблицу с каталогами (type_applications, type_tasks, status_applications)?
Я вижу решение для создания таблиц типов.
Типы таблиц
id name title type
1 game Games type_application
2 app Application type_application
3 arcade Arcade type_application
4 create Create App type_task
5 update Update App type_task
6 publish Publish App type_task
7 created Created application status
8 moderation Moderated application status
9 publish Application published status
В модели задач будет связь с типами.
public function type_application()
{
return $this->belongsTo(Type::class)->whereType('type_application');
}
public function type_task()
{
return $this->belongsTo(Type::class)->whereType('type_task');
}
public function status()
{
return $this->belongsTo(Type::class)->whereType('status');
}
Можете ли вы предложить больше решений этой задачи?
Может быть, какой-то шаблон проектирования подходит для этой задачи?