简体   繁体   English

Laravel 5.7,Eloquent:此集合实例上不存在属性 [X],多对多关系

[英]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.

相关问题 该集合实例laravel关系上不存在属性[X] - Property [X] does not exist on this collection instance laravel relationship Laravel 关系属性在此集合实例中不存在 - Laravel Relationship Property Does Not Exist In This Collection Instance 多对多关系“此集合实例上不存在属性[moedas]” - Relation Many to Many 'Property [moedas] does not exist on this collection instance ' laravel雄辩-属性[name]在此集合实例上不存在 - laravel eloquent - Property [name] does not exist on this collection instance Laravel Eloquent 多对多关系 - Laravel Eloquent relationship Many to Many Laravel HasManyThrough 返回属性 [X] 在此集合实例中不存在 - Laravel HasManyThrough Returns Property [X] does not exist on this collection instance 没有登录错误的Laravel 5.7身份验证:此集合实例上不存在属性[id] - Laravel 5.7 authentication without login error: Property [id] does not exist on this collection instance Eloquent 关系给出 [ticket_id] 在此集合实例上不存在 laravel - Eloquent relationship give [ticket_id] does not exist on this collection instance laravel Laravel/Eloquent belongsToMany - 属性 [people] 在 Eloquent 构建器实例上不存在 - Laravel/Eloquent belongsToMany - Property [people] does not exist on the Eloquent builder instance Laravel 5一对多雄辩的关系 - Laravel 5 one to many eloquent relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM