I'm making my very first laravel (and programming at all) project. I want to make multi-user authentification system with just two tables: users and roles The tables codes are shown below users
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles');
$table->string('password');
$table->rememberToken();
$table->timestamps();
roles
$table->increments('id');
$table->string('role');
The authentification is implemented using laravel command
php artisan make:auth
I've created midllewares admin, user1, user2 and user3 using laravel command
php artisan make:midlleware
How to implement middleware assigment based on role_id from users table that is assigned to user during registration? I had an idea to make something like this: in HomeController
public function __construct()
{
if($this->role_id=="1"){
$this->middleware('admin'); }
if($this->role_id=="2"){
$this->middleware('user0'); }
if($this->role_id=="3"){
$this->middleware('user1'); }
if($this->role_id=="4"){
$this->middleware('user2'); }
}
But I have an error Undefined property: App\Http\Controllers\HomeController::$role_id
Then I created __toString method in Controller
public function __toString()
{
return $this->role_id;
}
I try to echo $this to see it's format, I got an error Method App\Http\Controllers\HomeController::__toString() must not throw an exception, caught ErrorException: Undefined property: App\Http\Controllers\HomeController::$role_id
Am I on a good tracks? Or can anyone give me an advice what is the easiest way to implement multiuser auth but with no , or just one additional (permissions) table ?
Thanks