ShamiCanCode's avatar

Gate and multi role

I have two users admin and agent and there is a transaction table.In admin panel i want to show all transactions while in agent panel i want to show only those transactions made by logged in agent. I am restricting agent by not showing him that row but on the other hand pagination is working. Like if 100 rows of transactions in which agents transactions are 5 admin see these hundred rows but agent will also see the pagination.

0 likes
4 replies
rodrigo.pedra's avatar

How do you differentiate between admin and agent?

Is there a column on the user table? If so how is it called?

rodrigo.pedra's avatar
Level 56

@ShamiCanCode

Try this:

Route::get('/transactions', function (Request $request) {
    $transactions = Transaction::query()
        ->when(
            $request->user()->role === 'agent', 
            fn ($query) => $query->where('user_id', $request->user()->id),
        )
        ->latest()
        ->paginate();


    return view('transactions', [
        'transactions' => $transactions,
    ]);
});

The ->when() will only execute the callback if the first parameter evaluates to true.

Please or to participate in this conversation.