Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmcglone27's avatar

More table confusion

Learning how to retrieve data from db tables has been the biggest challenge for me. I'm not referring the simple default way.

Here I have a table of events, and I have no problem displaying everything in the table, but I want to also display the user who is associated with that event, but I'm thrown off by the id. I can get all the users, but not the user associated with the event with the id of 1. Also I can't use a foreach to loop over the data as we did in my previous question because this isn't an array.

So here's my index method. This is easy. I understand every bit of this code.

public function index()
    {
        $users = User::with('events')->get();

        return view('pages.events.index', compact( 'users'));
    }

But my head explodes when I get to the show method where I am trying to display the user associated with the event..

I have tried a several different ways that don't work. I'll copy my show method of each attempt individually below. but first I want to post output of tinker to show that the relationship is there, I just don't know how to write it.

>>> $user = User::find(1);
=> App\User {#4139
     id: 1,
     name: "David McGlone",
     email: "[email protected]",
     : null,
     img: null,
     created_at: "2020-09-04 17:50:47",
     updated_at: "2020-09-04 17:50:47",
   }
>>> $user->events
=> Illuminate\Database\Eloquent\Collection {#4137
     all: [
       App\Event {#4138
         id: 1,
         user_id: null,
         title: "First Event",
         date: "2008-11-01 00:00:00",
         description: "Organized by david User id - 1",
         location: "Home",
         remember_token: null,
         created_at: "2020-09-04 17:50:47",
         updated_at: "2020-09-04 17:50:47",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#4135
           user_id: 1,
           event_id: 1,
         },
       },
     ],
   }
>>> 

Event.php

public function events{

$this->hasMany('App\Event')

}

I found that this grabs the user, BUT it's grabbing the user with the same id as the event.

public function show($id)
    {       
        $events = Event::with('users')->findOrFail($id);

        var_dump($events);     
    }

Same as above.

public function show($id)
    {       
        $events = Event::with('users')->findOrFail($id)->get();

        var_dump($events);     
    }

I can't remember what this did, but i'm sure the experiences ones can see it was wrong.

public function show($id)
    {       
        $events = Event::findOrFail($id);
        $users = User::with('events')->get($id);
  
        return view('pages.events.show', compact( 'events', 'users'));
    }

0 likes
13 replies
ederson's avatar

You should show the relationship code too

But something looks strange (probably cause here is 05.30 😂)

In tinker you search for the user with id 1 and you get the user with ID 4.

1 like
dmcglone27's avatar

My bad, I copied the tinker and changed it to hopefully eliminate confusion with what I'm trying to explain. I'll fix that.

dmcglone27's avatar

It probably looks strange because it's wrong.. 😂

Snapey's avatar

Your relationships are messed up

Your event model has a user_id field (null) and also a pivot

You only have the pivot if the event is linked to many users and a user has many events

Is it a one-to-many or many-to-many relationship ?

dmcglone27's avatar

I started out with the idea an event would be able to have more than 1 user.

Then I changed it a couple times, trying to figure out what's going on... Now it feels like a jumbled up mess. and I'm confused.

I've looked for examples and cant' find any. I'm thinking there's a video by Jeff that covers this, but I don't know which one, because the video I was using to learn called "Roles and Abilities" doesn't go that far.

One good thing though, I probably watched that video 100,000 times in the last 2 weeks and I got really good with Tinker. lol

dmcglone27's avatar

Sorry guys, I just got home from Church.

I just looked into the first suggestions after replying to Snapey and It works. The problem seems to have been a combination of typo's and my relationship in my model was wrong. Here's the code.

My Model.

 public function user()
    {
        return $this->belongsTo('App\User');

    }

My Controller

   public function show($id)
    {
        $events = Event::with('user')->findOrFail($id);
        return view('pages.events.show', compact( 'events'));
    }

My View

{{$events->user->name}}

I'd like to ask.. Why is it if I change 'user' to 'users' in all 3 places, it fails? What am I missing?

dmcglone27's avatar

I don't know who to give the best answer to, because all 3 of you helped me realize my errors and got my train of thought in the right place.

Maybe the "best answer" should be a many-to-many relationship, because y'all rocked this one. :-)

Snapey's avatar

You should use user if event belongs to user

dmcglone27's avatar

If I'm following you correctly, what you mean is I should rename the relationship from $event to $user?

Snapey's avatar

no?

in the User model it should be events() plural because a user can have many

In the event model it shoukd br user() singular because an event belongs to a user

You always name relationships from the perspective of a single model

dmcglone27's avatar

Ah Ok. That does make much better sense. I'll do that.

Please or to participate in this conversation.