英文:
Attempt to read property "name" on int - Laravel
问题
我有一个Work模型,其中一个属性是requestor。
我试图获取请求数据,我可以获取:
- id
- user_id
- slug
但我试图获取user_id->name
,然后laravel返回错误:
试图在int上读取属性"name"
控制器:
$posts = Post::all();
return view('home', ['post' => $posts]);
视图:
<tbody>
@foreach ($post as $item)
<tr class="border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ $item->id}}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->user_id->name}}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->slug}}</td>
</tr>
@endforeach
</tbody>
Work模型:
public function User()
{
return $this->belongsTo(User::class);
}
英文:
I have a Work model where one of the attribute is a requestor.
I'm trying to obtain the request data and I can obtain
- id
- user_id
- slug
but I'm try to user_id->name
, and then laravel returns error with
> attempt to read property "name" on int
Controller:
$posts = Post::all();
return view('home', ['post' => $posts]);
View:
<tbody>
@foreach ($post as $item)
<tr class="border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ $item->id}}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->user_id->name}}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->slug}}</td>
</tr>
@endforeach
</tbody>
Work Model:
public function User()
{
return $this->belongsTo(User::class);
}
答案1
得分: 0
The error is explicit. The property user_id
is an integer and not a relationship object, hence you cannot access the attribute on it.
To do so, while returning the posts include the user along with the posts.
Controller:
$posts = Post::with('user')->get();
return view('home', ['posts' => $posts]);
This is assuming you have a relationship with the Post
and the User
model.
Post Model:
public function user()
{
return $this->belongsTo(User::class);
}
and now you may access the attribute name
from the user
in your view like:
<tbody>
@foreach ($posts as $item)
<tr class="border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ $item->id }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->user->name }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->slug }}</td>
</tr>
@endforeach
</tbody>
Documentation: Eager Loading
英文:
The error is explicit. The property user_id
is an integer and not a relationship object, hence you cannot access the attribute on it.
To do so, while returning the posts include the user along with the posts.
Controller:
$posts = Post::with('user')->get();
return view('home', ['posts' => $posts]);
This is assuming you have a relationship with the Post
and the User
model.
Post Model:
public function user()
{
return $this->belongsTo(User::class);
}
and now you may access the attribute name
from the user
in your view like:
<tbody>
@foreach ($posts as $item)
<tr class="border-b dark:border-neutral-500">
<td class="whitespace-nowrap px-6 py-4 font-medium">{{ $item->id }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->user->name }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $item->slug }}</td>
</tr>
@endforeach
</tbody>
Documentation: Eager Loading
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论