First of all, before posting any thread please see the preview of the thread, as it's hard to find the problem. Solution: You need to eager load the relationship and get the data from the relationship attribute and not the id.
$rapports = Rapport::with('Rapport')->all();
and in your view you can access it like
<td>{{$rapport->Rapport->nom}}</td>
One more suggestion that i want to give you is that, you should inverse the names of the relationship functions.
Thank you. I will try. where should I put this code you wrote
$rapports = Rapport::with('Rapport')->all();
i did and now i have this error
Call to undefined method Illuminate\Database\Eloquent\Builder::all()
Here is the error i get now with the get() method
ErrorException Attempt to read property "nom" on null (View: C:\wamp64\www\SET\resources\views\rapport\index.blade.php)
You have to define a custom value if the relationship id is not found on the target model like:
return $this->belongsTo(Agent::class)->withDefault([
'nom'=>'N/A',
]);
Hope your code works now :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community