I need get the name field in user table
My method in the controller:
public function reviewsactivity()
{
$activity = Activity::orderBy('id', 'DESC')->paginate();
$users = User::orderBy('id', 'ASC')->pluck('id', 'name');
return view('reviews.reviewsactivity', compact('activity', 'users'));
}
And the view:
@foreach($activity as $actividad)
<tr>
<td>{{ $actividad->description }}</td>
<td>{{ $actividad->subject_type }}</td>
<td>{{ $actividad->user->name }}</td>
<td>{{ $actividad->causer_type }}</td>
<td>{{ date('d-m-Y', strtotime($actividad->created_at)) }}</td>
</tr>
@endforeach
The field in the Activity table: causer_id
And I have the next log:
Trying to get property of non-object
(View: C:\laragon\www\tao\resources\views\reviews\reviewsactivity.blade.php)
Assuming you have
user_id
inactivities
table referencingusers
table then you can definebelongsTo
relationship inActivity
modelActivity Model
Now Fetch Activity with eager loading using
with('user')
Check Eager Loading documents here https://laravel.com/docs/5.6/eloquent-relationships#eager-loading