Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Created May 22, 2018 16:09
Show Gist options
  • Save jsdecena/f3fc6441717e2ef915913edd32e252be to your computer and use it in GitHub Desktop.
Save jsdecena/f3fc6441717e2ef915913edd32e252be to your computer and use it in GitHub Desktop.
users
- id
- type (string)
user_user
- user_id_1
- user_id_2
User extends Model {
....
public function agents()
{
return $this->belongsToMany(
User::class,
'user_user',
'user_id_1',
'user_id_2')
->where('type', '=', 'agent');
}
// and so on
....
}
// Then I can save it with
$agent = factory(User::class)->create(['type' => 'agent']);
User::agents()->save($agent);
// And output it with
return User::agents(); // Collection of users with agent type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment