1

I'm facing an issue with model extension. Here's an example of my problem:

I have a User model and an Admin model that extends my User model. I use a github repo called Bouncer for permissions. When I save my roles for an Admin model, it saves as /App/Admin and for Users, it saves as /App/User for the model reference.

So when I call my roles for a Admin or for a User, no problem. But my issue is when I want to query all my users with their roles. I obviously get all my users AND my admin, but the Admins can't get their roles because the are "/App/Admin" in the database.

How can I get all the roles of my "extended" models when I call the parent?

yivi
  • 42,438
  • 18
  • 116
  • 138
Martin C.
  • 11
  • 2

1 Answers1

0

You will create a relationship in your model using belongs to and use a method called "with ()".

First step: In your model, create belongs to ex:

public function post()
{
    return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}

Second step: You will use it in your controller,example:

$users = User::with('podcasts')->get();

Eloquent: Relationships https://laravel.com/docs/7.x/eloquent-relationships#updating-belongs-to-relationships

Another example Get Specific Columns Using “With()” Function in Laravel Eloquent