'Attempt to read property "email" on int - Laravel 8
I have a Work model where one of its attributes is the requestor, I am trying to obtain the requestor´s data (I can already obtain the primary key).
In this way I call the view:
$obras = Obra::all();
return view("obra.index", compact("obras"));
The view:
@forelse ($obras as $i)
<li>{{ $i->requestor_id->email }}</li>
@empty
The relationship in the work model:
public function requestor_id(){
return $this->hasOne(User::class);
}
Tables:
Users(applicants) have: id, name, email, password etc.
Work have: id, user_id, start_date etc.
Solution 1:[1]
I think it's because of the name of the relation. You can try to renaming it to requestor. Laravel has some internal behavior runing on underscore. It might return only the id.
public function requestor()
{
return $this->hasOne(User::class);
}
Solution 2:[2]
import use App\Models\Obra; in the Controller.
use App\Models\Obra;
$obras = Obra::all();
return view("obra.index", compact("obras"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | spzout |
| Solution 2 |
