Laravel哪个条件有条件?(Laravel where condition for one item?)

编程入门 行业动态 更新时间:2024-10-27 17:13:55
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();

最满意答案

看看哪里有

您可以使用whereHas和orWhereHas方法将“where”条件放在您的查询上。 这些方法允许您向关系约束添加自定义约束,例如检查注释的内容:

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

takle a look at whereHas

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:

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

更多推荐

本文发布于:2023-07-29 15:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   条件   Laravel   condition   item

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!