mercredi 2 juin 2021

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

I'm trying to put some session in my project and its working fine when Im logged out. But when Im logged in as an Admin Im getting Call to undefined method App\User::admin() errror.

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 everytime I try to redirect to other routes I get the same error except for the dashboard



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire