使用 eloquent 获取字段中具有数组值的记录


Get records with array values in a field using eloquent

我有一个表,它有一个我称之为videos的字段,该字段的类型是array 。现在我想获取 videos field 中至少有 1 个值的所有记录。下面是我的代码,它不返回我想要的内容。

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

我正在使用laravel 4.2和mongoDb作为我的数据库。

也许这个答案可以帮助你,因为我提出了Json对数组中的数据进行编码的解决方案。因此,当它为空时,该值将[]。所以你可以像这样查询它

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

我很高兴我找到了解决这个问题的方法。它需要知道字段videos存在而不是空。看看下面的代码:

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