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

wirli's avatar
Level 2

Two dimensions, in database table and view table

I have two dimensions in my cost control tool: account and project. The bookings table is accordingly:

        Schema::create('bookings', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('account_id')->unsigned();
            $table->foreign('account_id')->references('id')->on('accounts');
            $table->integer('project_id')->unsigned()->nullable();
            $table->foreign('project_id')->references('id')->on('projects');
            $table->date('booking_date');
            $table->decimal('amount', 9, 2);
        });

In my overview view I would like to show a table with the sum(amount) per accounts in rows and projects in columns (pivot). I assume the way to get the data is with a groupBy query:

        $balances = DB::table('bookings')
                    ->select('account_id', 'accounts.name',
                        'project_id', 'projects.name',
                        DB::raw('sum(amount) as balance')
                    ->join('accounts', 'bookings.account_id', '=', 'accounts.id')
                    ->leftJoin('projects', 'bookings.project_id', '=', 'projects_id')
                    ->groupBy('account_id', 'accounts.name', 'project_id', 'projects.name')
                    ->get();

But how do I translate that into an html-table with columns depending on the query-result?


@foreach($array as $array2)
 @foreach($array2 as $value)
  {!! $value !!}
 @endforeach
@endforeach

doesn't work, because the query result doesn't have a value for each cell.

Help is much appreciated.

0 likes
0 replies

Please or to participate in this conversation.