Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zione8036's avatar

admin auth expire

I just want to consult problem that I want to resolve. I tried to make a condition with the admin authentication. Whenever the admin auth expires, it always gives me an error message. I tried to resolve it by putting a condition that when the auth is false, it will directly route you to the dashboard, but still received that error. Whenever the admin auth expires, I have to type http://127.0.0.1:8000/admin/login for the nth time, and I don't want to see this error if I publish this website.

This is the Error ErrorException Attempt to read property "brand" on null (View: C:\xampp\htdocs\Advance Ecommerce Total Project.224\pro3\ecommerce\resources\views\admin\body\sidebar.blade.php)

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError C:\xampp\htdocs\Advance Ecommerce Total Project.224\pro3\ecommerce\resources\views/admin/body/sidebar.blade.php:37 @php

  $brand = (auth()->guard('admin')->user()->brand == 1);

  $category = (auth()->guard('admin')->user()->category == 1);

  $product = (auth()->guard('admin')->user()->product == 1);

  $slider = (auth()->guard('admin')->user()->slider == 1);

  $coupons = (auth()->guard('admin')->user()->coupons == 1);

  $shipping = (auth()->guard('admin')->user()->shipping == 1);

  $blog = (auth()->guard('admin')->user()->blog == 1);

  $setting = (auth()->guard('admin')->user()->setting == 1);

  $returnorder = (auth()->guard('admin')->user()->returnorder == 1);

  $review = (auth()->guard('admin')->user()->review == 1);

  $orders = (auth()->guard('admin')->user()->orders == 1);

  $infoPH = (auth()->guard('admin')->user()->infoPH == 1);

  $stock = (auth()->guard('admin')->user()->stock == 1);

  $reports = (auth()->guard('admin')->user()->reports == 1);

  $alluser = (auth()->guard('admin')->user()->alluser == 1);

  $adminuserrole = (auth()->guard('admin')->user()->adminuserrole == 1);
0 likes
2 replies
SilenceBringer's avatar
Level 55

@zione8036 you need to determine if user is authenticated first https://laravel.com/docs/9.x/authentication#determining-if-the-current-user-is-authenticated

auth()->guard('admin')->check()

or just check user() is not null

 $brand = auth()->guard('admin')->check() ? (auth()->guard('admin')->user()->brand == 1) : false;

// or

 $brand = auth()->guard('admin')->user() ? (auth()->guard('admin')->user()->brand == 1) : false;

by the way - this code piece is ugly. why not use foreach for all options if the names matches user props?

1 like

Please or to participate in this conversation.