【问题标题】:Laravel multiple table eloquent relationshipLaravel 多表雄辩关系
【发布时间】: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"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    选择时使用'with'建模以使返回对象具有您引用的数据

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

    但如果你想nested 关系使用 .(dot) in with

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 2017-10-17
      • 2016-11-26
      • 1970-01-01
      • 2016-09-06
      • 2015-03-14
      • 2018-11-28
      相关资源
      最近更新 更多