Kaye
Kaye

Reputation: 49

Call to undefined method App\User::admin() error in Session in Laravel

I'm trying to put some sessions in my project and it's working fine when I'm logged out. But when I'm logged in as an Admin I'm getting the following error:

Call to undefined method App\User::admin()

this is my routes

Route::resource('/create','PagesController@showCreate')->middleware(IsAdmin::class);
Route::get('/users','UserController@index')->middleware(IsAdmin::class);
Route::get('/verify','UserController@userVerify')->middleware(IsAdmin::class);

this is my User model

<?php

namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *  protected $table = 'users';
    *public $primaryKey ='id';
     * @var array
     */
  
    protected $fillable = [
        'name', 'email', 'password','userType',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'admin' => 'boolean',
    ];

    public function posts(){
        return $this->hasMany('App\Post');
    }

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

and this is my IsAdmin class

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        {
            if ( Auth::check() && Auth::user()->admin() )
            {
                return $next($request);
            }
    
            return redirect('home');
        }
    }
}

And every time I try to redirect to other routes I get the same error except for the dashboard

Upvotes: 0

Views: 1991

Answers (1)

ibrahim-dogan
ibrahim-dogan

Reputation: 621

Change if statement in your IsAdmin class:

if ( Auth::check() && Auth::user()->isAdmin() )

Upvotes: 1

Related Questions