Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chaitally's avatar

Adding extra information to session, based on user role, just after successful authorization

I am new to Laravel and I need help to resolve the problem I am facing. In my Laravel 5.4 application, I have role based users. There are 4 types of role:

  1. Admin
  2. School_admin
  3. Inspector
  4. Committee_member

Following are my models (for their respective database tables): class Role extends Model { protected $fillable = [ 'name', 'description', ]; public function users(){ return $this->hasMany('App\User')->withTimeStamps(); } }

class User extends Authenticatable { use Notifiable; protected $fillable = [ 'name', 'uname', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; public function roles(){ return $this->belongsToMany('App\Role')->withTimestamps(); } }

Now I have a third table called “block_inspectors” that contains extra information about the inspectors. Users only with role “Inspector” will have entries in this table. Other users will not have any records in this table.

Block_inspectors table fields:

  1. Id (primary key)
  2. User_id (foreign key mapping to the id column of users table)
  3. Designation
  4. Block_id

My Query: How do I fetch the extra information from the block_inspectors table once I know the logged in user has a role “Inspector”? I would like to keep this fetched information in the session so that I can use them in other controllers / views as and when required Currently I am using default Laravel Authentication system. It would be great if this can be achieved with minimum changes to the default login system.

0 likes
1 reply
lyleyboy's avatar

This one comes up a lot.

Check out the piviot() method. It look something like

$model->pivot()

Please or to participate in this conversation.