简体   繁体   English

此集合实例上不存在属性 [图像]。 / Laravel - with() 方法

[英]Property [image] does not exist on this collection instance. / Laravel - with() Method

I get data with relations from controller but can't use in blade我从 controller 获得了关系数据,但不能在刀片中使用

Controller.. Controller..

$employees = Post::where('user_id', $user_id)->with('people')->get();

Blade, I use in foreach like..刀片,我在 foreach 中使用,例如..

$employee->people->image

But it gives error但它给出了错误

Property [image] does not exist on this collection instance.此集合实例上不存在属性 [图像]。

When I dd($employees)当我 dd($employees)

在此处输入图像描述

the problem is that the variable $employee (which contains model Post btw) has relation 1:N to People I assume问题是变量$employee(包含model Post btw)与我假设的人有1:N的关系

meaning you have to loop the people collection returned by the relation这意味着您必须循环关系返回的人员集合

eg例如

@foreach($employees as $employee)
   @foreach($employee->people as $person)
      {{ $person->image }}
   @endforeach
@endforeach

You must foreach people relationship, you are getting collection with people for that post so you must foreach it because relationship return array with all people per post.您必须 foreach 人的关系,您正在收集该帖子的人,因此您必须 foreach 它,因为关系返回数组,每个帖子的所有人。

@foreach($employees as $employee)
   @foreach($employee->people as $user)
      {{ $user->image }}
   @endforeach
@endforeach

If you want only one from people you could use如果你只想要一个你可以使用的人

$employee->people->first()->image

Whin this way you should use你应该使用这种方式

@if( $employee->people->first()->image)
    {{ $employee->people->first()->image }}
@endif

Please check you post model does it have relation function defined?请检查您发布的 model 是否定义了关系 function?

public function people() {
        return $this->hasMany(people::class); // Add localID and foreign_key ID as param if requires
 }

Also check image property is there还要检查图像属性是否存在

Please check Property [title] does not exist on this collection instance hope this help请检查此集合实例上不存在属性 [标题]希望对您有所帮助

In this case, each post has a lot of people.在这种情况下,每个帖子都有很多人。 So, you need to specify from which people you want to get the information.因此,您需要指定要从哪些人那里获取信息。 For example, this code can be work:例如,此代码可以工作:

$employee->people->first()->image

Because you are getting the image from the first people.因为你从第一批人那里得到了图像。 But I don't recommend this code because you are getting all people of a post and use just one of them and it's not optimized.但我不推荐此代码,因为您正在获取所有人员的帖子并且只使用其中一个并且它没有经过优化。

But if you want to show all people use this:但是,如果您想向所有人展示使用此功能:

@foreach($employees as $employee) 
    @foreach($employee->people as $person)
        {{ $person->image }}
    @endforeach
@endforeach

暂无
暂无

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

相关问题 该集合实例上不存在属性[title]。 拉拉韦尔5.5 - Property [title] does not exist on this collection instance. laravel 5.5 (1/1)异常属性[price]在该集合实例上不存在。 (Laravel 5.4) - (1/1) Exception Property [price] does not exist on this collection instance. (Laravel 5.4) 此集合实例上不存在属性 [expiry_date]。 在 Laravel - Property [expiry_date] does not exist on this collection instance. in Laravel 该集合实例上不存在属性[associated_occupation]。 -Laravel - Property [associated_occupation] does not exist on this collection instance. - Laravel 此集合实例上不存在属性 [vegan]。 Laravel - Property [vegan] does not exist on this collection instance. Laravel 此集合实例上不存在属性 [ID]。 Laravel 表格错误 - Property [ID] does not exist on this collection instance. Laravel Form error “此集合实例上不存在属性 [角色]。” - "Property [role] does not exist on this collection instance." Laravel错误:此集合实例上不存在属性[id]。 但是它可以在本地服务器上运行 - Laravel Error: Property [id] does not exist on this collection instance. Yet it works on the local server 此集合实例上不存在属性 [order_number]。 拉拉维尔 9.x - Property [order_number] does not exist on this collection instance. laravel 9.x “此集合实例上不存在属性[registration_types]。” - “Property [registration_types] does not exist on this collection instance.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM