Reputation:
Hello there I need help in formatting my date before it reaches client side. Basically after getting the values from the database I want to format my date before it goes to the client side of my program like what I am doing here.
public function filterDataCal(Request $request) {
$filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->leftJoin('types', 'types.id', '=', 'methods.type_id')
->where('type_id', '=', $request->filters['type_id'])
->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
'types.id AS type_id_typetable', 'types.name AS type_name']);
if($filtered_table) {
// for ($i=0; $i < sizeof($filtered_table); $i++){
//
// // foreach ($filtered_table as $value[i]) {
// // $value[i]->created_at->format("m/d/Y h:i A");
// // }
// }
foreach($filtered_table as $data) {
$data->created_at = $data->created_at->format('m/d/y h:i A');
}
return $filtered_table;
} else {
return "";
}
}
I was not sure what kind of syntax I need to do to change the format of the date I have gotten from the database so what I did was foreach
them then $data->created_at = $data->created_at->format('m/d/y h:i A');
but alas it won't go through, could you help me with changing that property (created_at) to a different format? Maybe I have something wrong with my syntax or maybe I there's a different approach. Thank you.
Upvotes: 3
Views: 634
Reputation:
I ended up using a library called moment.js just to format the date in js
Upvotes: 0
Reputation: 2173
You need to return $data->created_at
instead $filtered_table
.
Let me tell you why, you formatted the date and stored it into the variable name says $data->created_at
but you are returning $filtered_table
which is actually the raw date (You never formatted.)
Hope it helps :)
Upvotes: 0
Reputation: 3266
There is nothing wrong in the syntax. You can simply print the date, you are returning the collecction.
foreach($filtered_table as $data) {
echo $data->created_at->format('m/d/y h:i A');
}
If you need to customize the format while returning a collection, you can set the $dateFormat
property on your model. See Date Mutators in documentation.
Upvotes: 1