У меня есть таблица, в которой есть некоторые данные о взаимосвязях, которые я могу получить, но мне также нужно получить связь из возвращенных данных. Это Shop database
, где меня интересует Shop name
. Я могу получить только shop_id
из таблицы shopDepartment
.
У меня есть следующие таблицы:
DailyList
- id
- grocery_id
- amount
- completed
Grocery
- id
- name
- measurement
ShopDepartments
- id
- shop_id
- name
- order
Grocery_ShopDepartment
- id
- grocery_id
- shop_department_id
DailyList_ShopDepartment
- id
- daily_list_id
- shop_department_id
DailyList_priority
- id
- daily_list_id
- priority
Shop
- id
- name
Это отношение от модели DailyList
class DailyList extends Model
{
protected $guarded = [];
public function dailylistpriority()
{
return $this->hasMany(DailyListPriority::class);
}
public function grocery()
{
return $this->belongsTo(Grocery::class);
}
public function shopDepartments()
{
return $this->belongsToMany(ShopDepartment::class) ;
}
}
DailylistController
public function index() // I need to have relationdata from shopdepartment
{
$list = DailyList::with(['grocery', 'shopdepartments', 'dailylistpriority'])->where('completed', 0)->get();
return $list;
}
Это мой вывод
{ "id": 550, "grocery_id": 108, "amount": 1, "completed": 0, "created_at": "2020-03-25 19:45:43", "updated_at": "2020-03-25 19:45:43", "grocery": { "id": 108, "name": "Havregryn", "measurement": "Stk", "created_at": "2020-02-03 09:26:23", "updated_at": "2020-02-03 09:26:23" }, "shopdepartments": [ { "id": 3, "shop_id": 1, "name": "Hygiejne & Pleje", "order": "3", "created_at": null, "updated_at": null, "pivot": { "daily_list_id": 550, "shop_department_id": 3 } }, { "id": 17, "shop_id": 2, "name": "Ukendt", "order": "0", "created_at": null, "updated_at": null, "pivot": { "daily_list_id": 550, "shop_department_id": 17 } } ], "dailylistpriority": [] }