尝试在整数上读取属性 “name” – Laravel

huangapple go评论48阅读模式
英文:

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-&gt;name, and then laravel returns error with

> attempt to read property "name" on int

Controller:

$posts = Post::all();
return view(&#39;home&#39;, [&#39;post&#39; =&gt; $posts]);

View:

&lt;tbody&gt;
@foreach ($post as $item)
  &lt;tr class=&quot;border-b dark:border-neutral-500&quot;&gt;
     &lt;td class=&quot;whitespace-nowrap px-6 py-4 font-medium&quot;&gt;{{ $item-&gt;id}}&lt;/td&gt;
     &lt;td class=&quot;whitespace-nowrap px-6 py-4&quot;&gt;{{ $item-&gt;user_id-&gt;name}}&lt;/td&gt;
     &lt;td class=&quot;whitespace-nowrap px-6 py-4&quot;&gt;{{ $item-&gt;slug}}&lt;/td&gt;
  &lt;/tr&gt;
@endforeach
&lt;/tbody&gt;

Work Model:

public function User()
{
    return $this-&gt;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(&#39;user&#39;)-&gt;get();
return view(&#39;home&#39;, [&#39;posts&#39; =&gt; $posts]);

This is assuming you have a relationship with the Post and the User model.

Post Model:

public function user()
{
    return $this-&gt;belongsTo(User::class);
}

and now you may access the attribute name from the user in your view like:

&lt;tbody&gt;
@foreach ($posts as $item)
&lt;tr class=&quot;border-b dark:border-neutral-500&quot;&gt;
    &lt;td class=&quot;whitespace-nowrap px-6 py-4 font-medium&quot;&gt;{{ $item-&gt;id }}&lt;/td&gt;
    &lt;td class=&quot;whitespace-nowrap px-6 py-4&quot;&gt;{{ $item-&gt;user-&gt;name }}&lt;/td&gt;
    &lt;td class=&quot;whitespace-nowrap px-6 py-4&quot;&gt;{{ $item-&gt;slug }}&lt;/td&gt;
&lt;/tr&gt;
@endforeach
&lt;/tbody&gt;

Documentation: Eager Loading

huangapple
  • 本文由 发表于 2023年5月18日 12:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定