使用eloquent在字段中获取包含数组值的记录

时间:2016-01-27 03:11:40

标签: php mongodb laravel laravel-4 eloquent

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

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

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

2 个答案:

答案 0 :(得分:0)

当我提出request.get('http://jsonplaceholder.typicode.com/posts/1', function(error, response, body){ if(!error && response.statusCode == 200){ console.log(body); } }); 对数组中的数据进行编码的解决方案时,这个答案可能对您有所帮助。因此,当它为空时,该值将为Json。所以你可以查询类似这样的东西

[]

答案 1 :(得分:0)

我很高兴我找到了解决方法。它需要知道字段@model Mvc.Affiliates.Models.MyProducts @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Details</h2> <br /> <div style="height:200px; width:100%"> @using (Html.BeginForm("Index","Home", FormMethod.Post)) { <h2>Details</h2> @Html.LabelFor(model => model.ProductId) @Html.TextBoxFor(model => model.ProductId) @Html.LabelFor(model => model.ProductName) @Html.EditorFor(model => model.ProductName) @Html.ValidationMessageFor(model => model.ProductName, "*") @Html.CheckBoxFor(model => model.HaveExperiance) @Html.ValidationMessageFor(model => model.HaveExperiance, "*") <input type="submit" value="Submit" /> } </div> 存在而不是空的。看看下面的代码:

videos