【问题标题】:Laravel where condition for one item?Laravel 哪里有一件物品的条件?
【发布时间】:2016-09-05 03:17:56
【问题描述】:

我有Products 表和Categories 表。

每个产品可能有一个或多个类别。为此我创建了表:products_caetgories

那么,我如何设置where 条件,其中product_category = 1?

我试过这样:

Product::with("categories")->where("product_category", 1)->get();

【问题讨论】:

标签: laravel-5.2 laravel-5.3


【解决方案1】:

看看whereHas

您可以使用 whereHas 和 orWhereHas 方法来放置“where” 您的条件有查询。这些方法允许您添加 对关系约束的自定义约束,例如检查 评论内容:

Product::with("categories")->whereHas("categories", function($query){
 $query->where('id','=',1);
})->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 2019-11-27
    相关资源
    最近更新 更多