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

johnw65's avatar

Report Query for all of the Controllers

I need to run a global query and based on their database role, they will be able to see a list of reports that they have access to. So for each controller, what is the best way, to run a query from a global query and return a list of records (which are report listing) so they can see it in their blade file as a view. For example: for FinancialController, I would need to run a a standard query, but because of the different database role, they will only see reports that Financial department have access to.

for LogisticsController, I would need to run a standard query, but see reports that only Logistic deparment have access to.

Please note its a same query, but because due to database role, the listings are different.

The plan is to have a report button in the blade file with a list of reports that they have access to and run.

Is there a best method for achieving this, instead of running the same query for each controller. Thanks in advance.

0 likes
5 replies
LaryAI's avatar
Level 58

One solution to this problem would be to create a middleware that runs the global query and adds the resulting report listings to the request object. This middleware can then be applied to all controllers that need access to the report listings.

Here's an example implementation:

  1. Create a middleware class that runs the global query and adds the report listings to the request object:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class AddReportListingsToRequest
{
    public function handle($request, Closure $next)
    {
        $user = Auth::user();
        $role = $user->role; // assuming the user's role is stored in a 'role' column

        // run the global query based on the user's role
        $reportListings = DB::table('reports')
            ->where('department', $role)
            ->get();

        // add the report listings to the request object
        $request->merge(['reportListings' => $reportListings]);

        return $next($request);
    }
}
  1. Apply the middleware to all controllers that need access to the report listings:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FinancialController extends Controller
{
    public function __construct()
    {
        $this->middleware('addReportListingsToRequest');
    }

    public function index(Request $request)
    {
        $reportListings = $request->reportListings;

        // use the report listings in the view
        return view('financial.index', compact('reportListings'));
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LogisticsController extends Controller
{
    public function __construct()
    {
        $this->middleware('addReportListingsToRequest');
    }

    public function index(Request $request)
    {
        $reportListings = $request->reportListings;

        // use the report listings in the view
        return view('logistics.index', compact('reportListings'));
    }
}

This way, the global query is only run once for each request, and the resulting report listings are available to all controllers that need them.

Snapey's avatar
Snapey
Best Answer
Level 122

first thing to decide is how you will identify which reports can be run by which role.

If you have a reports table with parameters for the report, you could have a pivot table that lists role_id and report_id. You can then create a list of reports with role->reports() query

This is assuming that some reports are of use to more than one role. If it is that each report is specific to a role then you can do it Lary's way - but it would be quite inflexible.

johnw65's avatar

@Snapey, I'm going to try the your way since $role would dictate who can view the report. Thank you.

$reportListings = DB::table('reports') ->where('department', $role) ->get();

Snapey's avatar

@johnw65 what did you think I was suggesting, if not by role?

What do you do if multiple roles want to have access to the same report?

johnw65's avatar

@Snapey, I meant your way. Multiple people accessing would not be an issue since the report that they are running is from a SQL statement that my DBA has created. So it's very similar to multiple users trying to access a record, however, it runs a SQL statement, and get exported out to an Excel Spreadsheet. Apologize for the confusion.

Please or to participate in this conversation.