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

murtaza1904's avatar

To show all stocks with respective dates in their respective columns.

I am creating an inventory management system. How can I get sum of singular date in one column (such as sum of all stocks in on 1 October) and so with other dates.

<table class="table table-bordered table-striped table-sm table-hover" id="product_table">
                    <thead class="table-dark">
                        <tr class="text-center">
                            <th scope="col">Name</th>
                            @foreach ($stockins as $stockin)
                                <th scope="col">{{ $stockin->created_at->format('d-M-Y') }}</th>
                            @endforeach
                            <th scope="col">Total IN</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="text-center">
                            @foreach ($stockins->unique('product_id') as $stockin)
                            <td>{{ $stockin->product->product_name }}</td>
                            @endforeach
                            @foreach ($stockins as $stockin)
                                <td>{{ $stockin->total_in }}</td>
                            @endforeach
                        </tr>
                    </tbody>
                </table>
0 likes
9 replies
MohamedTammam's avatar
Stock::select(DB::raw('SUM("total"), created_at'))->groupBy(DB::raw('DATE(created_at)'));

I assumed you want to calculate a column name total

murtaza1904's avatar

No, I just want total of number of entries incurred on each dates in their columns.

murtaza1904's avatar

I think I am a bit confused in making you explain. In simpler words, I want only one date to be displayed with its total if it has more than one record, for example in the image path below, where I have 3 records of 1 october 2022 i just want them in one column with its total. I want same for the other dates.

/*Stocks page image */ https://drive.google.com/file/d/1um_MU26K58PzsEZBOWWOCxHkN836wJyD/view?usp=sharing

/* Database image*/ https://drive.google.com/file/d/17tT1X3aJlzpE3YwwIvNkDbxK-GbBuk25/view?usp=sharing

MohamedTammam's avatar

@murtaza1904 Then I assume you have the product model, right?

$product->load('stocks', function($query) {
	$query->select('product_id', DB::raw('COUNT("id") as count, created_at'))
		->groupBy(DB::raw('DATE(created_at)'))
})

$product->stocks // ...
murtaza1904's avatar

@MohamedTammam you mean like this?

$product = Product::all();
        $product->load('stocks', function($query) {
            $query->select('product_id', DB::raw('COUNT("id") as count, created_at'))
                ->groupBy(DB::raw('DATE(created_at)'));
        });
MohamedTammam's avatar

@murtaza1904 Try that

$product = Product::all();
        $product->load(['stocks' => function($query) {
            $query->select('product_id', DB::raw('COUNT("id") as count, created_at'))
                ->groupBy(DB::raw('DATE(created_at)'));
        }]);

Please or to participate in this conversation.