1

I have a function in my User model which is called isAdmin, if "Admin" is set to 1 in the database, it returns true, if not it returns false.

How is this gonna work with Auth::user()?

When I do Auth::user()->isAdmin(), it returns "Property [admin] does not exist on this collection instance."

Thats why I came to the conclusion it may not use the User model?

User model

public function isAdmin() {
   if($this->admin == 1) {
        return true;
     } else {
        return false;
   }
}
public function view () 
    {
        if(Auth::check() && Auth::user()->isAdmin()) {
            $user  = User::all();
            $post  = Post::all();
            $visit = Visits::all();


            return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
        } else {
            return redirect()->to('/');
        }
    }
15
  • use Auth::user()->isAdmin(); Commented Nov 16, 2019 at 10:09
  • Sorry, I misstyped in my question. I am already using Auth::user()->isAdmin()) Commented Nov 16, 2019 at 10:11
  • @CunnertA share the code from your isAdmin method, as obviously you are checking for a property to exist on a collection instance. Commented Nov 16, 2019 at 10:13
  • I have added my code Commented Nov 16, 2019 at 10:17
  • Are you sure that the error occurs when you do this? Have you checked dd(Auth::user()->isAdmin()); what is the result? As to me it seems like you are trying somewhere on User::all()->isAdmin(); Commented Nov 16, 2019 at 10:19

3 Answers 3

1

If I may suggest, for this use case, you can actually make do without an extra function. You could just say auth()->user()->admin, specially if the 'admin' column in the database is boolean type.

Otherwise (even admin column is not boolean type) you can set up a mutator method in the model, like so:

public function getIsAdminAttribute()
{
   return (bool) $this->admin;
}

Then to check you can access it like so: Auth::user()->isAdmin or auth()->user()->isAdmin

And better yet, you might want to read about Gate and Policies to achieve more robust access controlling. https://laravel.com/docs/5.7/authorization

Sign up to request clarification or add additional context in comments.

7 Comments

Calling is_admin or admin does not make a difference, the OP mentioned that wants to use a method :) + instead of casting it like that the OP can just add protected $casts = ['admin' => 'boolean']; in the model, it will achieve the same.
@nakov good catch. casts array is yet another method for casting and satisfies my main concern (first point) of having an extra method.
Anyhow 1 = true and 0 = false in PHP, so there is no need for casting unless the user needs to show true or false somewhere :) The error is really obvious, but the user gone away :)
Thank you! I have made it a bool and changed my method. So much cleaner :)
@CunnertA I really want you to teach me how this solved your problem that you've shared above :D
|
0

Suggestion, change the code to just this:

public function isAdmin() {
    return $this->admin;
}

This code does exactly the same as you've got above..

Now in your admin.blade.php you are using:

$user->isAdmin();

But in the controller you have:

$user  = User::all();

which returns collection.

You should iterate over it, and check on each user instance if it is an admin:

$users  = User::all();

In the view:

@foreach($users as $user)
 @if($user->isAdmin())
    {{ $user->name }} // some code here..
 @endif
@endforeach

Comments

0

No Need To do anything just check if login then auth()->check() is return true then auth()->user() return the user

public function view () 
    {
        if(auth()->check() && auth()->user()->isAdmin()) {
            $user  = User::all();
            $post  = Post::all();
            $visit = Visits::all();


            return view('admin')->with('post', $post)->with('user', $user)->with('visit', $visit);
        } else {
            return redirect()->to('/');
        }
    }
public function isAdmin()
    {
        return $this->admin;

    }

5 Comments

tbh I like the way of using a function, its much cleaner for me.
i change my answer.please check it
There's no need of else block here in the view() method as the return statement is used in the if block and the execution won't go to else if the if statement is true. If the if statement is false, the execution will jump to return redirect()->to('/'); anyways.
i just repair his fuction & auth()->user()
@albus_severus The else block does not make any sense here. Because the return statement immediately terminates the execution of a function when it is called from within that function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.