Support the ongoing development of Laravel.io →
Authentication Packages
Last updated 2 years ago.
0

I am new to Laravel and Entrust and this is one posible way:

in UserRepository.php class after "public function signup($input)"

    $this->save($user);
    //add
    $insertedId = $user->id;
    $user = User::find($insertedId);
    $default_role= Role::where('name', 'Default')->get()->first();
    $user->attachRole( $default_role);

I bet there is better solution for this, but I didn't found it.

0

A better way could be assigning role with the help of events!

  • Create 'events.php' file under 'app' folder
<?php
Event::listen('user.signup', function($user)
{
	$userRole = DB::table('roles')->where('name', '=', 'User')->pluck('id');
	$user->roles()->attach($userRole);
	return false;
});
  • Include events.php file from start/global.php
require app_path().'/events.php';
  • Fire the event on signup (From your user controller's sign up function)
Event::fire('user.signup', array($user));
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ledzgio ledzgio Joined 25 Apr 2014

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.