0

I used the scaffolding tools to generate my authentication code for my laravel project. I created a UserController to make a profile page which works great but when I try to make a function that can be used on Auth::user() i get an error Call to undefined method Illuminate\Database\Query\Builder::admin()

Why isn't the admin function accessible on the Auth::user()? Doesn't that extend my UserController? Or am I mixing it up with the model? Is the the model a good place to check if my user is an admin?

Here is my user controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;

class UserController extends Controller
{

    /**
     * Create a new user controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * @return View with user data
     */
    public function index() {
        $user = Auth::user();

        return view('users.index', compact('user'));
    }

    /**
     * @return bool
     * Returns bool if the user is an admin.
     */
    public function admin() {
        $user = Auth::user();

        $authorized_users = [
            '[email protected]'
        ];

        return array_key_exists($user->email, $authorized_users);
    }
}

and I am calling it on a different route controller function

public function index() {
    return Auth::user()->admin();
}

I am fairly new to laravel and php so any critique is valuable and wanted!

2 Answers 2

1

You could add a function or attribute to you User model, I prefer attributes:

//User.php
class User extends Model{
    protected $appends = ['is_admin'];
    public function getIsAdminAttribute()
    {
        $user = Auth::user();

        $authorized_users = [
            '[email protected]'
        ];

        return array_key_exists($user->email, $authorized_users);
    }
    ...
}
//Then in your view 
Auth::user()->is_admin
Sign up to request clarification or add additional context in comments.

Comments

0

No, Auth::user() does not extends any Controller. It represents the instance of the currently logged in/authenticated user. It will allow you retrieve other attributes of the use such as id, name etc Auth::user()->admin(); does not make any sense. Auth::user() has nothing to do with the UserController or any other controller.

3 Comments

Oh okay. That makes sense actually. Durp. So the correct way would be to create some type of helper function. I want to access an isAdmin function for my views.
Yes using helper is the correct way for defining custom function. Here's a good tutorial for creating custom helper
Hey @jonju, I was wondering how the scope resolution operator finds the user() method. And where is it located?

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.