Support the ongoing development of Laravel.io →
Eloquent Laravel Database
Last updated 9 months ago.
0

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.

0

Thank you. I will try. where should I put this code you wrote

$rapports = Rapport::with('Rapport')->all();

Last updated by @maccladder 10 months ago.
0

i did and now i have this error

Call to undefined method Illuminate\Database\Eloquent\Builder::all()

0

try to use get() method instead of all()

0

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)

0

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 :)

0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 Laravel.io - All rights reserved.