gpt4 book ai didi

mysql - 四表查询

转载 作者:行者123 更新时间:2023-11-29 18:04:55 25 4
gpt4 key购买 nike

我想通过一个查询从四个表中获取所有数据。我的 table

类别

id

name

子类别

id

name

类别_子类别

id

category_id

subcategory_id

项目

id

name

category_subcategory_id

一个产品可以属于一个类别和子类别,我在类别和子类别之间有查询,并且想添加到此项目表中。

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subcategory extends Model
{
protected $fillable = [
'id',
'name',
];

protected $table = 'subcategories';


public function categoriesTwoLevel()
{
return $this->belongsToMany(CategoriesTwoLevel::class, 'categories_subcategories', 'subcategories_id','categories_id');
}
}


class Categories extends Model
{
protected $fillable = [
'id',
'name',
];

protected $table = 'categories_two_level';

protected $timestamp = false;

public function subcategory()
{
return $this->belongsToMany(Subcategory::class, 'categories_subcategories', 'categories_id', 'subcategories_id')->withPivot('id');
}

}

这是一个查询,它通过表categories_subcategories从Subcategory和Category表中获取所有数据

CategoriesTwoLevel::with('subcategory')->get();

但是表格项目不可用。

最佳答案

使用dot notation to load nested relationships数据:

Categories::with('subcategory.categoriesTwoLevel.items')->get();

如果您正确定义了所有这些关系,这将起作用。

关于mysql - 四表查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com