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

murtaza1904's avatar

I want to achieve the following thing with eloquent model

/* Image link */

https://drive.google.com/file/d/1um_MU26K58PzsEZBOWWOCxHkN836wJyD/view

In the above image I have a table with product name and stocks-In dates and total in. On 1 October three stocks added , on 4 October 1 stock added and on 18 October 1 stock added. Know I want to show only unique dates means 1 October date is repeating because on that day three stocks added, I want to show repeated dates once and the sum of their stocks in quantity below it. At the end I want to show the sum of all the stock in dates quantity in total in column.

/Database Image/

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

Controller code

public function render()
    {
        return view('livewire.reports.report-list', [
            'stockins' => StockIn::oldest()->get(),
        ]);
    }

view code

<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
8 replies
SilenceBringer's avatar

@murtaza1904 use groupBy

StockIn::oldest()
	->select(
		'id',
		'product_id', 
		DB::raw('sum(total_in) as total')
	)	
	->groupByRaw('product_id, date(created_at)')
	->with('product')
	->get()
	->groupBy('product_id');

The problem with your table is that different product can have different dates for stock in. How you want to display it? if, for example, product 1 have 1 and 3 October, but product 2 - 2 and 5 October?

murtaza1904's avatar

@SilenceBringer In first colomn I want to display product name than in other columns all stock in dates related to that product in and in quantity of stock in added that date and in last column sum of all stocks of that product

SilenceBringer's avatar

@murtaza1904 for one product - no problem

but or many - you'll need to have separated tables for each

$stockIns = StockIn::oldest()
	->select(
		'id',
		'product_id', 
		DB::raw('sum(total_in) as total'),
		DB::raw('date(created_at) as date_created')
	)	
	->groupByRaw('product_id, date(created_at)')
	->with('product')
	->get()
	->groupBy('product_id');
@foreach ($stockIns as $productStockIns)
				<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 ($productStockIns as $stockIn)
                                <th scope="col">{{ $stockin->date_created }}</th>
                            @endforeach
                            <th scope="col">Total IN</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="text-center">
                            <td>{{ $stockIn->first()->product->product_name }}</td>
                            @foreach ($productStockIns as $stockIn)
                                <td>{{ $stockin->total }}</td>
                            @endforeach
                            <td>{{ $productStockIns->sum('total') }}</td>
                        </tr>
                    </tbody>
                </table>
@endforeach
murtaza1904's avatar

@SilenceBringer getting this error

SQLSTATE[42000]: Syntax error or access violation: 1055 'db_inventorymanagementsystem.stock_ins.id' isn't in GROUP BY

/* Error link */

https://flareapp.io/share/J7owwNk7
SilenceBringer's avatar
Level 55

@murtaza1904 sure, just remove id from select

$stockIns = StockIn::oldest()
	->select(
		'product_id', 
		DB::raw('sum(total_in) as total'),
		DB::raw('date(created_at) as date_created')
	)	
	->groupByRaw('product_id, date(created_at)')
	->with('product')
	->get()
	->groupBy('product_id');
murtaza1904's avatar

@SilenceBringer One more thing I want to achieve is that I have stocks out table and after total in I wan to show stock out dates and total out.

Please or to participate in this conversation.