Is it possible to get two different datasets with Laravel eloquent orm from a table?

Viewed 47

I need to compare the data in the table by years. For this, I want the user to enter two different years. I want to print the result in the table.

I'm pulling a year's worth of data like this:

$order_detail_model = OrderDetail::whereBetween('created_at', [Carbon::createFromDate($request->get('year1')->startOfYear()),Carbon::createFromDate($request->get('year1')->endOfYear()]);
$order_detail_model = $order_detail_model->with('product')->select('service_id', $order_detail_model->raw('count(*) as total'),$order_detail_model->raw('sum(price) as price'))->groupBy('service_id');

When I print with dd($order_detail_model): enter image description here

I want to pull data from two different years in the same query like this. Is it possible? Or is there another solution?

Note:Should be in a single query

1 Answers

user OrWhereBetween alongside whereBetween pass your second date in orWhereBetween

if you want to group by service_id

groupByRaw('YEAR(created_at),service_id')
Related