I'm trying add dependency injection on User class and after few hours trying, I found this way:
- I added this code to
App\Models\User
class
public function __construct()
{
$this->roleService = resolve(RoleService::class);
}
- I've checked
$this->roleService
value, it work normally, buut when I try to use it in a function:
public function isAdmin() {
return $this->roleService->isAdmin($this->role_id);
}
- This function throw an error
Call to a member function isAdmin() on null
. When I logged$this->roleService
, it returnednull
.
Anyone can solve this?