Title: Laravel Relations - Many to one
Tags: laravel;foreign-keys
Question: On the index page, I want to show the last 10 events. Every event has 4 participants (called users).
Event table example:
id, name, user_one, user_two, user_three, user_four, event_start_time, more_event_data...
In the controller I'm sending event data to the view:
public function index()
if( !AuthUserHasPermissionToRoute( 'event.index' ) )
abort( 404 );

return view( 'event.index' )
-&gt;with('event', Event75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0orderBy('id', 'DESC')-&gt;get() );
Using the model I would like to send the user data as well
In the EventModel, I have tried hasMany (but will only with 1)
public function users()
return $this-&gt;hasMany( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass, 'id', 'user_one', 'id' );
And a Many-To-Many:
public function users()
return $this-&gt;belongsToMany( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass );
In the view, I'm trying to get the users name ( later possibly more ) to be displayed
{{ $game-&gt;users-&gt;user_one-&gt;name }}
{{ $game-&gt;users-&gt;user_two-&gt;name }}
{{ $game-&gt;users-&gt;user_three-&gt;name }}
{{ $game-&gt;users-&gt;user_four-&gt;name }}
But I can not get it to work, Could somebody show me what I'm doing wrong?


Here is another answer: If your event table have user_one, user_two, user_three, user_four user ids, then for the event table the relation should be belongsTo
Event Model
public function user_one()
return $this-&gt;belongsTo( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass,'user_one', 'id');
public function user_two()
return $this-&gt;belongsTo( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass,'user_two', 'id');
public function user_three()
return $this-&gt;belongsTo( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass,'user_three', 'id');
public function user_four()
return $this-&gt;belongsTo( User75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0lass,'user_four', 'id');
I would recommend make a different table as user_events and make belongsToMany relationship with it
Comment for this answer: Worked thanks! But I think you are right. Will rebuild to use an extra table
Comment for this answer: Glad to help you :) Happy coding.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:10/07/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<laravel><foreign-keys>', 'Title': 'Laravel Relations - Many to one', 'ViewCount': 29, 'id': 64245148, 'user_AboutMe': '', 'user_AccountId': 5839598.0, 'user_DisplayName': 'VMeijer', 'user_Reputation': 396.0, 'user_Views': 50.0, 'user_id': 4601619.0}}