gpt4 book ai didi

php - hasManyThrough 不工作

转载 作者:行者123 更新时间:2023-12-03 20:19:12 25 4
gpt4 key购买 nike

我有这张 table ,

categories: id | name | slug
Products: id | name | slug | category_id | brand_id
Brands: id | name | slug |

当我尝试将品牌放在功能类别中时会出现问题。

分类型号
public function brands()
{
return $this->hasManyThrough('App\Brand', 'App\Product' 'brand_id', 'id');
}

该品牌要么没有出现,要么显示在错误的类别中。

有没有其他方法可以通过品牌产品?

最佳答案

改变你们的关系如下:

public function brands()
{
return $this->hasManyThrough('App\Brand', 'App\Product' 'category_id','id','brand_id');
}

但是,我怀疑您是否可以使用 hasManyThrough()检索此关系。
laravel site 所示,你的表应该有以下关系。
Category 1-* Product 1-* Brand这在您的情况下是不可能的。

我认为您必须使用以下内容手动执行此操作:
public function Brands($category_id){
$products = Category::find($category_id)->Products()->get()->pluck('brand_id');
$brands = Brands::whereIn('id','=',$products)->get();
return $brands;
}

关于php - hasManyThrough 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39066377/

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