简体   繁体   English

雄辩地获取字段中具有数组值的记录

[英]Get records with array values in a field using eloquent

I have this table that has a field I called videos and the type of that field is array . 我有此表,其中有一个我称为videos的字段,该字段的类型为array Now I want to get all the records that have at least 1 value inside videos field . 现在,我想获取所有在videos field中至少具有1值的记录。 Below is my code that doesn't return what I want. 下面是我的代码,没有返回我想要的。

$prod = Product::where(array('videos' => array('$ne' => 0)))->get();

I am using laravel 4.2 and mongoDb as my database. 我正在使用laravel 4.2和mongoDb作为我的数据库。

May be this answer could help you as I came up with the solution that Json encodes the data in array. 当我想出Json将数据编码为数组的解决方案时,这个答案可能对您有帮助。 So when it's empty the value will be [] . 因此,当它为空时,其值为[] So you can query it something like this 所以你可以这样查询

$prod = Product::where('videos', '!=', '[]')->get();

I am glad that I found the solution to this. 我很高兴找到了解决方案。 It needs to know that field videos exists and not empty. 它需要知道现场videos存在并且不为空。 Take a look at below code: 看下面的代码:

$prod = Product::where(array('videos' => array('$ne' => [], '$exists' => true)))->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM