【问题标题】:Filter pivot table in Laravel?在 Laravel 中过滤数据透视表?
【发布时间】:2021-12-20 18:07:50
【问题描述】:

有没有办法在pivot table 内的pivot table 的一个表上添加where 子句?例如在下面的代码中,如何在products_categories 中添加where 子句来指定productstype='ELECTRONICS'

DB::table("products_categories")
    ->whereIn("category_id", array_values($ids))
    ->pluck("id")
    ->all();

【问题讨论】:

标签: laravel


【解决方案1】:

试试这个:

$type = "ELECTRONICS";
$products_categories = Product::with('categories')->whereHas('categories', function($q) use ($type){
    $q->where('type', "%$type%");
})->get();

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 2017-10-17
    • 2016-02-12
    • 2019-09-17
    • 2014-01-12
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多