繁体   English   中英

Laravel 多表 eloquent 关系

[英]Laravel multiple table eloquent relationship

我有三个名为用户、菜单项、类别的表

用户表:

+-----------------------+
|       Users           |
+====+======+===========+
| id | name | type      |
+----+------+-----------+

餐厅餐桌:

+-----------------------+
|       Restaurant      |
+====+======+===========+
| id | name | user_id   |
+----+------+-----------+

菜单项表:

+------------------------------------------+
|       Menu_items                         |
+====+======+==============================+
| id | cat_id | user_id  |  restaurant_id  |
+----+------+------------------------------+

类别表:

+-----------------------+
|       categories      |
+====+======+===========+
| id | name             |
+----+------+-----------+

我的关系 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');
    }
}

用户 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');
    }
}

类别 model

class Category extends Model
{
    public $table = 'categories';
    public  function menuitems()
    {
        return $this->hasMany('App\Models\Menuitems', 'main_category', 'id');
    }
}

菜单项 model

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"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

我应该如何达到这个可以有人请指导我。

当 select 使返回 object 具有您所指的数据时,对 model 使用 'with'

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

但如果你想嵌套关系使用。(点) in with

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

暂无
暂无

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

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