I have a SQL Query in my controller, It fetches all data from a table to sort it by date. There are some duplicate entries of sku_parent in table with different id, I want to fetch the latest entry of each sku_parent according to 'date'. This is my DB:
+----+-----------+------------+-----------+--------------+------------+---------------------+---------------------+
| id | warehouse | sku_parent | sku_child | case_balance | date | created_at | updated_at |
+----+-----------+------------+-----------+--------------+------------+---------------------+---------------------+
| 5 | SeaWest | 120 | 120 - 25 | 400 | 2020-08-26 | 2020-08-26 19:02:20 | 2020-08-26 19:02:20 |
| 6 | SeaWest | 121 | 121 - 25 | 784 | 2020-08-26 | 2020-08-26 19:02:42 | 2020-08-26 19:02:42 |
| 7 | SeaWest | 121 | 121 - 25 | 734 | 2020-08-26 | 2020-08-26 19:03:46 | 2020-08-26 19:03:46 |
| 8 | SeaWest | 120 | 120 - 25 | 350 | 2020-08-26 | 2020-08-26 19:03:46 | 2020-08-26 19:03:46 |
+----+-----------+------------+-----------+--------------+------------+---------------------+---------------------+
This is what I have done so far:
$data = DB::table('logs')
->where('sku_parent', $request->sku)
->where('id', \DB::raw("(select max(`id`) from logs)"))
->whereBetween('date', array($request->from_date, $request->to_date))
->get();
This code only fetches the max id from table and does not shows other product name. I want to get each product name having the max id.