【问题标题】:How do I make a selection from two related tables?如何从两个相关表中进行选择?
【发布时间】:2022-01-24 22:31:29
【问题描述】:

我有一个产品表,与类别表和集合表的关系是多对多的。我需要按类别和系列进行选择,以便获得其中包含的产品

我写了一些选择,但没有考虑到产品与收藏和类别相关。

$filter_collections = [1, 3];
$filter_categories = [2, 5];


$products = Product::whereHas('collection', function ($query) use ($filter_collections) {
                $query->whereIn('collection_id', $filter_collections);
            })->whereHas('category', function ($query) use ($filter_categories) {
                $query->whereIn('category_id', $filter_categories);
            })->get();

如何让选中的商品属于指定的分类和收藏?

【问题讨论】:

    标签: php sql laravel eloquent laravel-8


    【解决方案1】:

    未经测试,但请您试试这个:

    $products = Product::
      whereIn('collection_id', $filter_collections)
    ->orWhereIn('category_id', $filter_categories)
    ->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多