Laravel call method after loading the model

I am trying to check the model constructor if the authenticated user is allowed access to this model, but I find that $ this from the constructor context is empty. Where are the attributes assigned to the model in Laravel, and how do I access the method call after loading all the attributes?

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    var_dump($this); // empty model
    $this->checkAccessible();

}

Greetings in advance

+4
source share
1 answer

you can use the controller filter to check whether the user is logged in or not, and how you call any function of the model.

public function __construct(array $attributes = []){     
  $this->beforeFilter('auth', array('except' => 'login')); //login route

   if(Auth::user()){
        $user_id = Auth::user()->user_id;
        $model = new Model($attributes);
        //$model = User::find($user_id);
   }
 }

Constructor model binding attributes

Model.php

public function __construct(array $attributes = array())
{
    $this->setRawAttributes($attributes, true);
    parent::__construct($attributes);
}
0
source

All Articles