簡體   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