[英]Laravel 5.7, Eloquent: Property [X] does not exist on this collection instance, Many to Many Relationship
I got this two tables (Estado(Status) and Proyecto(Proyect)), that have a pivot table in between called EstadoProyecto(statusProyect), I'm trying to access only the proyects that have either 2 or 3 in their id_status(id_estado) field, and that have the most recent date.我得到了这两个表(Estado(Status)和 Proyecto(Proyect)),它们之间有一个 pivot 表,称为 EstadoProyecto(statusProyect),我试图仅访问 id_status(id_estado)中有 2 或 3 的项目) 字段,并且具有最近的日期。 It Works when using find(using either 2 or 3), but it gives me the error.
它在使用 find(使用 2 或 3)时有效,但它给了我错误。 " Eloquent: Property [proyectos] does not exist on this collection instance, Many to Many Relationship" when trying to send an array to the find method, or when using the get method.
“ Eloquent:此集合实例上不存在属性 [proyectos],多对多关系”尝试将数组发送到 find 方法或使用 get 方法时。 It gives me the same error when I'm not using the find method with only one value.
当我不使用只有一个值的 find 方法时,它给了我同样的错误。 Here are my models, and the query I'm trying to use.
这是我的模型,以及我正在尝试使用的查询。 model1 model2 model3
模型1模型2 模型3
This is the query that works right now: query Thanks in advance.这是现在有效的查询:查询提前致谢。 Diego:)
迭戈:)
Your query is returning a collection because there may be multiple entries for your model returned by the find
method that Eloquent provides so you could either use the first
method like this:您的查询返回一个集合,因为 Eloquent 提供的
find
方法返回的 model 可能有多个条目,因此您可以使用first
一种方法,如下所示:
$estados = Estado::whereHas('proyectos', function ($query) {
$query->whereIn('id_estado', [2, 3])
->orderBy('fecha_estado', 'DESC');
})
->with('proyectos', function ($query) {
$query->whereIn('id_estado', [2, 3])
->orderBy('fecha_estado', 'DESC');
})
->first();
With the above snippet, to get multiple results that match those conditions you would only need to replace the first
method with get
使用上面的代码片段,要获得与这些条件匹配的多个结果,您只需将
first
方法替换为get
The above code with give you can an instance of a single Estado
model.上面的代码为您提供了单个
Estado
model 的实例。
Otherwise, if you are going to use something along the original approach you can use get
and return a collection, you can iterate over them in a foreach loop:否则,如果您打算使用原始方法中的某些东西,您可以使用
get
并返回一个集合,您可以在 foreach 循环中迭代它们:
$estados = Estado::whereHas('proyectos', function ($query) {
$query->orderBy('fecha_estado', 'DESC');
})
->with('proyectos')
->get();
foreach ($estados as $estado) {
$estado->proyectos->id;
}
Or you could access the items in the collection like you would an array, granted you know that you definitely have the entry required in your results:或者您可以像访问数组一样访问集合中的项目,前提是您知道结果中肯定有所需的条目:
$estados[0]->proyectos->id;
If you are looking to get just one result, using Eloquent's first
method is often the best option for this.如果您只想获得一个结果,那么使用 Eloquent 的
first
一种方法通常是最好的选择。
I hope this helps!我希望这有帮助!
Extra info:额外信息:
The find
method in Eloquent can accept either a single value or an array of values as an argument and in the case of either no value being passed to it or an array of values being passed to it, a Collection may be returned rather than an instance of a single model. Eloquent 中的
find
方法可以接受单个值或值数组作为参数,并且在没有传递给它的值或传递给它的值数组的情况下,可能会返回 Collection 而不是实例单个 model。
The get
method on the other hand can be used without supplying any arguments and will return collection every time (although these can still hold no items if the underlying query did not yield any results)另一方面,可以在不提供任何 arguments 的情况下使用
get
方法,并且每次都会返回集合(尽管如果基础查询没有产生任何结果,这些仍然可以不包含任何项目)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.