1

Currently using the standard users auth for Laravel (php artisan make:auth) and then combining it with spatie/laravel-permission.

My question is as follows, I have different types of users

  • Students
  • Bursary Administrators
  • Bursary Providers
  • Super Admins

I have 'spatie' roles for the above, and can assign them to the various users. Keeping the current users table / system intact, how would I go about assigning a 'type' to the users. Should I go ahead and setup a 'morphOne' type relationship (as is seen here) or should I just setup a tinyint field which defines the type (0=SuperAdmin,1=Student, etc etc)?

//edit Similarly, I also need to store the parent it belongs to. That is to say, a Bursary Administrator can have many users, a Bursary Provider can have many users. I need to store the parent_id for the user. So this then means I should rather go with the relationship route and add a "userable" table and store:

  • userable_type = model type (bursary administrator / provider/ student)
  • userable_id = parent id

//edit 2

So now I have the following alterations:

//Bursary Administrators & Bursary Providers models have:
public function users() {
    return $this->morphMany('App\Users', 'userable');
}

Created a migration to alter the Users table:

    Schema::table('users', function (Blueprint $table) {
        $table->integer('userable_id')->index('userable_id_index');
        $table->string('userable_type');
    });

And made the necessary User model addition:

public function userable()
{
    return $this->morphTo();
}

My question has now morphed to be; as follows:

I can get a list of Bursary Provider users by doing:

$bp = App\BursaryProviders::find(1);
foreach ($bp->users as $user) {
    //
}

Let's assume a Bursary Provider user is logged in and they create a new user; how do I get the ID of the currently logged in user's Bursary Provider so that the created user has the same Bursary Provider?

ie:

user_id = 123
& its bursary_provider_id = 1

user_id(123) creates user_id(124) 
// How do I assign user_id(124) to have its bursary_provider_id=1 ?
apokryfos
  • 38,771
  • 9
  • 70
  • 114
SupaMonkey
  • 876
  • 2
  • 9
  • 25
  • 1
    Why do you need a role and type which will be the same? Keeping that aside, in most cases for readability and clean code its better to have a `oneToMany` relation instead of a number or enum column, which will be harder to know what it is. – thefallen Jul 10 '19 at 11:28
  • Sorry, I left an important part out - refer to edit bit at the end. I need to store the parent id so my assumption is then that I need to alter the users table to store the type and id and use the relation as you mentioned – SupaMonkey Jul 10 '19 at 12:27

0 Answers0