Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 质疑拉威尔的雄辩关系_Php_Mysql_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 质疑拉威尔的雄辩关系

Php 质疑拉威尔的雄辩关系,php,mysql,laravel,laravel-5,eloquent,Php,Mysql,Laravel,Laravel 5,Eloquent,我有三张桌子 类别 产品 品牌 我在分类表中与以下产品有关联: public function products() { return $this->belongsToMany('App\Product','product_sub_categories','subcategory_id','product_id'); } public function manuf() { return $this->belongsTo('App\Brand','

我有三张桌子

  • 类别
  • 产品
  • 品牌
我在分类表中与以下产品有关联:

public function products()
{
    return $this->belongsToMany('App\Product','product_sub_categories','subcategory_id','product_id');
}
public function manuf()
    {
        return $this->belongsTo('App\Brand','brand');
    }
我在产品表上与以下品牌有关联:

public function products()
{
    return $this->belongsToMany('App\Product','product_sub_categories','subcategory_id','product_id');
}
public function manuf()
    {
        return $this->belongsTo('App\Brand','brand');
    }
我正在查询categories表,以按某个品牌返回该类别的产品

例如。

我想看看菲亚特品牌在汽车类的所有产品

我试过以下方法,但我觉得我遗漏了什么

 $search = 'fiat';
 $products = $category->products()->where(function($query) use ($search){
                    $query->brand->name = $search;
                })->get();
按某一品牌退回该类别的产品

我假设您知道品牌ID和类别ID,并且产品和类别具有多对多关系(因为您使用的是
belongTomany
),并且产品属于品牌:

Product::where('brand_id', $brandId)
       ->whereHas('categories', function($q) use(categoryId) {
           $q->where('id', $categoryId);
       })
       ->get();

我遇到的问题是,我的产品与许多类别相关,并由products_categories associations表定义-它仍然有效吗?@Dev.Wol是的,如果两个关系都定义正确,它将有效。