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

lara32bd's avatar

Gate::authorize() with Livewire pagination

Recently I created a new livewire component replacing my laravel controller, in controller method I use

Gate::authorize('user-activity'); which works fine.

Same Gate::authorize('user-activity') works fine with the livewire component using pagination. I could easily see the page. However, when I click the next or previous page it shows 'Not authorzied' default page I set for the app, if I refresh the page /activity?page=5 it works again.

url /activity -> works fine url /activity?page=5 -> works fine with browser refresh but not next/prev button on the pagination link.

What I'm doing wrong here? I assume I need to set Gate::authorize('user-activity') in another function, internally used by pagination next/prev action?

 public function render()
    {
        Gate::authorize('user-activity');
        $user = User::find($this->id);

        return view('livewire.activity-detail', [
            'username' => $user->username,
            'activities' => UserActivity::where([['user_id', '=', $user->id], ['company_id', '=', $user->company_id]])->orderBy('created_at', 'desc')->paginate(50)
        ])
        ->extends('layouts.admin')
        ->slot('content');
    }
0 likes
5 replies
lara32bd's avatar

I had to add my middleware class, Permission::class in the AppServiceProvider'a boot() function with Livewire

public function boot(): void
    {
        Livewire::addPersistentMiddleware([
            Permission::class,
        ]);
    }

This is somewhat interesting cause my web.php has route group which already defines this, the ccy.admin is the Permission::class middleware. So I thought I wouldn't need this.

 Route::group(['middleware' => ['auth',  'ccy.admin']], function () {
	Route::get('user/activity', Activity::class)->name('user-activity');
}

Having route like this which linked with Livewire component doesn't actually run my custom middleware, I wondering why!!!

Anyway problem solved, still best experience with Livewire. Here is the full code of the Livewire componenet.

<?php

namespace App\Livewire;

use App\Models\UserActivity;
use Livewire\Component;
use Livewire\WithPagination;
use Gate;
use Illuminate\Support\Facades\Auth;

class Activity extends Component
{
    use WithPagination;

    protected $paginationTheme = 'bootstrap';

    public function render()
    {
        Gate::authorize('user-activity');

        $user = Auth::user();
        return view('livewire.activity', [
            'activities'  => UserActivity::where('company_id', '=', $user->company_id)->with('user')->orderBy('created_at', 'desc')->paginate(30),
        ])
        ->extends('layouts.admin')
        ->slot('content');
    }
}

MohamedTammam's avatar
Level 51

@lara32bd I didn't know you're relaying custom middleware,If you have custom middleware you need to add it to Livewire to Livewire Service provider.

However, if you are applying a custom middleware from your application the initial page-load, and want it persisted between Livewire requests, you will need to add it to this list from a Service Provider in your app.

https://livewire.laravel.com/docs/security#configuring-persistent-middleware

Please or to participate in this conversation.