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

scsilver's avatar

Authorization on new guard

I define new admin guard which has driver is eloquent in config/auth.php. And I am using it to Authenticate App\Admin normally.
I also create new BookingPolicy and register in app\Providers\AuthServiceProvider.php:

    protected $policies = [
        'BookingPolicy' => 'App\Policies\Admin\BookingPolicy',
    ];

In BookingPolicy, I have edit method:

    public function edit(\App\Admin $admin)
    {
        $groupCanEdit = [Admin::SUPERADMIN, Admin::ADMIN, Admin::ORGANIZER];

        return in_array($admin->group_id, $groupCanEdit);
    }

In Admin\BookingController, I have edit method:

    public function edit($id)
    {
        $this->authorize('edit', 'BookingPolicy');

        $booking = Booking::find($id);

        return view('admin.booking.edit', ['booking' => $booking]);
    }

But when I visit localhost/admin/booking/1/edit:

Type error: Argument 1 passed to App\Policies\Admin\BookingPolicy::edit() must be an instance of App\Admin, instance of App\User given

After digging into the core files. I found it ship with default guard automatically in Illuminate\Auth\AuthServiceProvider.php.
How can I change that behaviour base on the middleware because I'm using auth.admin in admin/* routes. Or how can I pass App\Admin to method instead of App\User. Or another way to achive the goal.
I'd appreciate any ideas. Thanks

0 likes
2 replies

Please or to participate in this conversation.