简体   繁体   English

Laravel 多表 eloquent 关系

[英]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.我应该如何达到这个可以有人请指导我。

Use 'with' to model when select to make return object have data that you refer to当 select 使返回 object 具有您所指的数据时,对 model 使用 'with'

Restaurants::with('Menuitems')->get();

but if you want to nested the relation use.(dot) in with但如果你想嵌套关系使用。(点) in with

Restaurants::with('Menuitems.Categories')->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM