1

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

ITgirl
  • 31
  • 3

2 Answers2

1

You can't access the role_id because it's a property on your User model, not on your controller. That's what the "Undefined property" error means. $this refers to the Controller object, not the User model.

\Auth::user()->role_id should work, granted that you've added the role_id attribute to the User model's $fillable array.

Loek
  • 4,037
  • 19
  • 35
  • I've added role_id as $fillable attribute to User's model. You mean that instead $this->role_id I put \Auth::user()->role_id ? When I replace, and add use Illuminate\Support\Facades\Auth; to the HomeController header I got this error Trying to get property 'role_id' of non-object – ITgirl Dec 20 '18 at 12:05
  • Look into Gates. It's a very easy way to determine permission through a laravel app. – Brad Goldsmith Dec 20 '18 at 12:06
1

Firstly, you tried to use without define $role_id in this HomeController Class.

Second, you can't use auth middleware when before finish constructing, because constructors are created before middlewares, that's why its returning null.check here

use Auth;

class Homecontroller extends Controller{

   private $role_id;

   public function __construct(){

   }

   public function __toString(){
      $authUser = Auth::user(); // or Auth::user()->role_id;
      $this->role_id = $authUser->role_id;

      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'); 
      }

      return $this->role_id;
   }
}

and another way should be with this middleware-parameters

Happy coding ~!

JsWizard
  • 1,663
  • 3
  • 21
  • 48