My problem is that I want to get data form the database table from the created_at
attributes as per year and month only. The code I have tried is:
$post= Mjblog::select(DB::raw('YEAR(created_at) year, MONTH(created_at) month')); $posts_by_y_m = $post->where('created_at',$post)->get();
You can use ->whereMonth('created_at', $month) also. Both will work.
Try to do something like this: $date1 = Carbon::today()->toDateString(); $date2 = Carbon::today()->toDateString(); $myModel = MyModel::find(1); $myModel->whereBetween('created_at', [$date1, $date2]); $myModel->get();
Laravel Get Last Month records Use the below laravel eloquent query to get the last month records from the database table. User::whereMonth('created_at', '=', Carbon::now()->subMonth()->month)->get(['name','created_at']); This query uses laravel method whereMonth() and get().
There are date helpers available in the query builder:
$post = Mjblog::whereYear('created_at', '=', $year) ->whereMonth('created_at', '=', $month) ->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With