Table Builder: Query repository to only show certain results (not a filter)
Created 5 years ago by finnito

Hey all!

I've got a question: I would like to customise a table in the admin panel to only show certain results depending on the user that is logged in. I would like this to not be a filter because I only want to show entries associated with a community (stored in another stream) for which the user is an "admin". Therefore they shouldn't have the option to view another communities entries, only their own.

Is this possible?

Thanks 😄

ryanthompson  —  5 years ago Best Answer

You can use the querying callback to start the query off right. An onQuerying method on your builder would do the trick. This method is resolved out of the IoC container so the builder is passed but you can method inject anything:

     * Fired just before querying
     * for table entries.
     * @param Builder $query
    public function onQuerying(Builder $query)
        $user = auth()->user(); // Or dispatch a command

        $query->where('user_id', $user->getId());

        $query->orderBy('updated_at', 'ASC');
        $query->orderBy('created_at', 'ASC');
finnito  —  5 years ago

Works a treat, thanks as always @ryanthompson!