[英]Laravel 4 - get Records where entry in N:N Relation exists
I have some trouble with an Eloquent Query. 我有一个雄辩的查询麻烦。
I Have 3 Tables: 我有3张桌子:
Now i am trying to an get random result set from the Product table, but only records where an media file exists. 现在,我尝试从“产品”表中获取随机结果集,但仅记录媒体文件所在的位置。
Something like this (does not work..) 这样的事情(不起作用..)
Product::orderByRaw("RAND()")->media()->wherePivot('file', '=!' , '')->take(10)->get();
You should use: 您应该使用:
Product::orderByRaw("RAND()")->has('media')->take(10)->get();
It will take all products that has at least one media. 它将使用至少具有一种介质的所有产品。 I assume that you store in
product_media
only entries where file != ''
我假设您仅在
product_media
存储file != ''
条目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.