Mohib
Mohib

Reputation: 467

how to write a custom function in User model in laravel

I'm using laravel 5.4, in my User.php file I want to write a function like this :

public function isAdmin(Request $request)
    {
        if ($request->user()->id == 1) {
            return true;
        }
    }

The function I want to use in my middleware and my blade file. How to write this ? Now it's giving me this error :

Type error: Argument 1 passed to App\User::isAdmin() must be an instance of Illuminate\Http\Request, none given, called in /home/mohib/MEGA/Projects/saifullah-website/app/Http/Middleware/isAdmin.php on line 18

Upvotes: 0

Views: 1310

Answers (1)

Mozammil
Mozammil

Reputation: 8750

If you want to find out if the currently authenticated user is an Administrator, based on your logic, you could do something like this:

In App\User.php

public function isAdmin() 
{
    if ($this->id == 1) 
    {
        return true;
    }

    return false;
}

and then, you can use it like this:

if(Auth::check() && Auth::user()->isAdmin())
{
    // do something here
}

Upvotes: 2

Related Questions