1
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();

This return something like array format

[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]

I want to isolate DATE and point_no. For example

$year=['2015','2016','2017','2018','2019','2020'];

I tried $date['DOCDATE'] to isolate date it's not working. Is there any better way to fix this??

1
  • I don't understand how the title goes with your question? Do you actually want a string or do you want another array with modified values? Commented Jun 17, 2021 at 13:54

3 Answers 3

2
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();

$date_array = array();

foreach($date as $dat){
  array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}

return $date_array;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_map function.

$extractYear = function($date) {
    return substr($date["DATE"], 0, 4);
};

$years = array_map($extractYear, $date);

Comments

1

try this query -

$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
    return [Carbon::parse($q->DATE)->format('Y')];
});

3 Comments

Carbon showing error undefined type how to import this
use Carbon\Carbon; -- add this to the top of the controller.
its worked But i want showing my table date instead of current date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.