Laravel 关系数据 return
Laravel relationship data return
我有一个 table,它有一些关系数据,我可以很好地返回这些数据,但我也需要从返回的数据中获得关系。这是 Shop database
,我对 Shop name
感兴趣。我只能从 shopDepartment
table 中检索 shop_id
。
我的 table 是:
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": [] }
在你的 ShopDepartment.php
class.
class ShopDepartment {
$table = 'ShopDepartments';
public function Shop()
{
return $this->belongsTo(Shop::class);
}
}
然后您可以在 DailyList 查询中包含嵌套关系。
$list = DailyList::with(['grocery', 'shopdepartments.shop', 'dailylistpriority'])->where('completed', 0)->get();
我有一个 table,它有一些关系数据,我可以很好地返回这些数据,但我也需要从返回的数据中获得关系。这是 Shop database
,我对 Shop name
感兴趣。我只能从 shopDepartment
table 中检索 shop_id
。
我的 table 是:
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": [] }
在你的 ShopDepartment.php
class.
class ShopDepartment {
$table = 'ShopDepartments';
public function Shop()
{
return $this->belongsTo(Shop::class);
}
}
然后您可以在 DailyList 查询中包含嵌套关系。
$list = DailyList::with(['grocery', 'shopdepartments.shop', 'dailylistpriority'])->where('completed', 0)->get();