I have a three tables, Users, Events and Attends. Simply put, users which attend events are stored in the attends table. users and events have the id column, while Attends has id, user_id, and event_id. I want to pass to the profile of the user a list of the events s/he attends (as Event objects, as I want to get both the name and the id of an event). I created models for all three tables (User, Event, Attend)
the code in the UserProfileController is as follows:
$userAttendsObjectArray = Attend::where('user_id',$userId);
$userEventsIds = [];
foreach($userAttendsObjectArray as $userAttendsObject){
$userEventsIds[] = $userAttendsObject->event_id;
}
$userEvents = Event::where('id',$userEventsIds);
return view('pages.profile', compact('user','userEvents'));
The profile.blade.php part of the code is:
if(count($userEvents) != 0)
@else
He didn't attend any event
@endif
The message "He didn't attend any event" doesn't show up, yet no event names are shown either. I don't understand.
The attends table have one line with user 59 attending event 16 (both the users with id 59 and the event with the id 16 exist in the database)