Laravel哪个条件有条件?(Laravel where condition for one item?)
我有Products表和Categories表。
每个产品可能有一个或多个类别。 为此,我创建了表: products_caetgories 。
那么,我怎么能设置product_category = 1的条件呢?
我试着像:
Product::with("categories")->where("product_category", 1)->get();I have Products table and Categories table.
Each of products may have one or more cateories. For this I created table: products_caetgories.
So, how I can to set where condition where product_category = 1?
I tried like as:
Product::with("categories")->where("product_category", 1)->get();最满意答案
看看哪里有
Product::with("categories")->whereHas("categories", function($query){ $query->where('id','=',1); })->get();您可以使用whereHas和orWhereHas方法将“where”条件放在您的查询上。 这些方法允许您向关系约束添加自定义约束,例如检查注释的内容:
takle a look at whereHas
Product::with("categories")->whereHas("categories", function($query){ $query->where('id','=',1); })->get();you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries. These methods allow you to add customized constraints to a relationship constraint, such as checking the content of a comment:
更多推荐
发布评论