【发布时间】:2021-03-03 17:50:06
【问题描述】:
我有三个表,分别命名为用户、菜单项、类别
用户表:
+-----------------------+
| Users |
+====+======+===========+
| id | name | type |
+----+------+-----------+
餐厅餐桌:
+-----------------------+
| Restaurant |
+====+======+===========+
| id | name | user_id |
+----+------+-----------+
菜单项表:
+------------------------------------------+
| Menu_items |
+====+======+==============================+
| id | cat_id | user_id | restaurant_id |
+----+------+------------------------------+
分类表:
+-----------------------+
| categories |
+====+======+===========+
| id | name |
+----+------+-----------+
我的关系模型函数在这里。
餐厅模型
class Restaurants extends Model
{
public $table = 'restaurants';
public function getFoodItems()
{
return $this->hasMany('App\Models\Menuitems','restaurant_id','id');
}
public function getCatGroup()
{
return $this->getFoodItems()->groupBy('main_category');
}
}
用户模型
class Chefs extends Model
{
public $table = 'users';
public function restaurants()
{
return $this->hasMany(Restaurants::class, 'vendor_id', 'id');
}
public function getVendorFoodDetails()
{
return $this->hasMany('App\Models\Menuitems','vendor_id','vendor_id');
}
}
类别模型
class Category extends Model
{
public $table = 'categories';
public function menuitems()
{
return $this->hasMany('App\Models\Menuitems', 'main_category', 'id');
}
}
菜单项模型
class Menuitems extends Model
{
protected $table = "menu_items";
public function categories()
{
return $this->belongsTo(Category::class, 'main_category', 'id');
}
}
我的回答应该是这样的。
{
"id": 13,
"name": "Vendor",
"restaurants": [
{
"id": 3,
"vendor_id": 13,
"name": "Restaurant Name",
"get_cat_group": [
{
"id": 1,
"main_category": 1,
"categories": {
"id": 1,
"name": "test11111",
"menuitems": [
{
"id": 1,
"name": "Masala test",
},
{
"id": 8,
"name": "Masala Channa"
}
]
}
},
{
"id": 7,
"main_category": 2,
"categories": {
"id": 2,
"name": "tests2",
"menuitems": [
{
"id": 7,
"name": "Masala Channa"
}
]
}
}
]
}
]
}
我应该如何达到这个可以有人请指导我。
【问题讨论】: