PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How to fix Call to a member function isAdmin() on null

 October 19, 2022     admin, laravel, php, roles     No comments   

Issue

I have a route : Route::get('/TransactionHistory','QuoteController@transactionHistory');

In Controller QuoteController, I have a function as:

public function transactionHistory(){
         if(Auth::user()->isAdmin())
        {
               $res= DB::table('products_products')
->select('users_users.display_name','products_products.name','products_products.id','products_products.user_id','products_products.quantity_type','buy_product.price','buy_product.rating','buy_product.mileage_name','buy_product.id as buy_id')
->join('buy_product','products_products.id','=','buy_product.product_id')
->join('users_users','buy_product.user_id','=','users_users.id')
->get();
        }
        else{
       $u_id=auth()->user()->id;
      $res= DB::table('products_products')
->select('products_products.name','products_products.id','products_products.user_id','products_products.quantity_type','buy_product.price','buy_product.rating','buy_product.mileage_name','buy_product.id as buy_id')
->join('buy_product','products_products.id','=','buy_product.product_id')

->where('buy_product.user_id','=',$u_id)
->get();}
        
    return view('templates::pagetwigs/transaction-history')->with('details',$res);
    } 

On routing the path I got error as :Call to a member function isAdmin() on null

How to fix this.


Solution

Add middleware in the route or in the constructor of controller as shown:

public function __construct()
{
      $this->middleware('auth');
    
}

This will not allow you to access the page unless you are logged in . This will always check that Auth::user() is not null



Answered By - Manmeet Singh Raina
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing