I have two models and migration tables named as users and roles (for both migrations and models names).
This is the first time I am using boot(). I have one doubt: if the user is successfully registered then his role should be stored automatically in the roles table as admin, for that please help me I am trying some ways it's not working.
User.php
public static function boot()
{
parent::boot();
Static::updating(function (){});
// Here I am unable to figure out how to pass roles model or migration take data and how to update the values
}
create_roles_table.php
$table->string('role');
$table->unsignedInteger('user_id');
$table->foriegn('user_id')->references ('id')->on('users');
Many users can be 'admin', yes? Then the
rolestable should have the following structure.If an user can only have one role, then the
userstable should have a foreign key to therolestable.When an user is registered, laravel fires the event
Illuminate\Auth\Events\Registered.You can listen to it in your
App/Providers/EventServiceProviderclass.If you want to use the User Model's events instead, it should look like this:
or using
booted()insteadIf an user can have many roles, a separate migration is needed.
role_idno longer needs to be on theuserstable.Then you need to define a relationship in both
UserandRolemodelsAs for updating the role when creating/registering an user an user, replace
by