i want to get the value name in created_at field
i have the code like this
public function index()
{
$month = ['01','02','03','04'];
// $month = ['january','february','march','april'];
$user = [];
foreach ($month as $key => $value) {
$user[] = User::where(\DB::raw("DATE_FORMAT(created_at, '%m')"),$value)->count();
}
return view('AdmPelatihan.Dashboard.index')->with('month',json_encode($month))->with('user',json_encode($user));
}
i want make it like 01 = januari, dst
You are looking for the whereMonth()
method of the query builder.
Your code can be simplified to:
public function index()
{
$months = ['01','02','03','04'];
$users = [];
foreach ($months as $month) {
$users[] = User::whereMonth('created_at', $month)->count();
}
return view('AdmPelatihan.Dashboard.index')->with([
'months' => json_encode($months),
'users' => json_encode($users)
]);
}
To be clear, this would apply to these months in any year.
If you want the month names in your view, you could also do something like:
public function index()
{
...
$monthNames = collect($months)->transform(function ($month) {
return \Carbon\Carbon::parse('2021-'.$month.'-01')->format('F');
})->toArray();
return view('AdmPelatihan.Dashboard.index')->with([
'months' => json_encode($months),
'monthNames' => json_encode($monthNames),
'users' => json_encode($users)
]);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments