[英]Laravel multiple table eloquent relationship
I have three tables named as Users, Menu items, Category我有三个名为用户、菜单项、类别的表
Users table:用户表:
+-----------------------+
| Users |
+====+======+===========+
| id | name | type |
+----+------+-----------+
Restaurant table:餐厅餐桌:
+-----------------------+
| Restaurant |
+====+======+===========+
| id | name | user_id |
+----+------+-----------+
Menu items table:菜单项表:
+------------------------------------------+
| Menu_items |
+====+======+==============================+
| id | cat_id | user_id | restaurant_id |
+----+------+------------------------------+
Category table:类别表:
+-----------------------+
| categories |
+====+======+===========+
| id | name |
+----+------+-----------+
And my relationships model functions are here.我的关系 model 函数在这里。
Restaurant model餐厅 model
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');
}
}
User model用户 model
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');
}
}
Category model类别 model
class Category extends Model
{
public $table = 'categories';
public function menuitems()
{
return $this->hasMany('App\Models\Menuitems', 'main_category', 'id');
}
}
Menu items model菜单项 model
class Menuitems extends Model
{
protected $table = "menu_items";
public function categories()
{
return $this->belongsTo(Category::class, 'main_category', 'id');
}
}
and my response should be something like this below.我的回答应该是这样的。
{
"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"
}
]
}
}
]
}
]
}
How should I reach this can some one please guide me.我应该如何达到这个可以有人请指导我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.