Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent
Last updated 2 years ago.
0
Solution

erdely said:

I have following tables:

  1. users (classic laravel table)
  2. events
  3. user_events
// event model

public function userEvents()
{
  return $this->hasMany('App\Models\UserEvent');
}

// user event model

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

public function event()
{
  return $this->belongsTo('App\Models\Event');
}

// example of resultset
$user_events = UserEvent:get();

foreach ($user_events as $user_event)
{
  echo $user_event->user->name;           // displays the user's name that is associated with user_event
  echo $user_event->event->name;         // displays the even'ts name that is associated with user_event
}

hope that helps

0

It works, many thanks :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

erdely erdely Joined 7 Nov 2016

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.

© 2024 Laravel.io - All rights reserved.