-1

I am trying to make my resource return the data in this way (1) 2020-11-26 / 2020-11-27 (id) data_from / data_to is there any way to do that?

public function toArray($request)
{
    $parent_array = parent::toArray($request);

    $return_array = [];
    $return_array['id'] = $parent_array['id'];
    $return_array['text'] = $parent_array['data_from'];
    $return_array['text2'] = $parent_array['data_to'];

    return $return_array;
}
{
    "response": {
        "id": 1,
        "text": "2020-11-26",
        "text2": "2020-11-27"
    }
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Gene_Nostrada
  • 114
  • 1
  • 6

1 Answers1

0
return sprintf('(%s) %s / %s', $parent_array['id'], $parent_array['data_from'], $parent_array['data_to']);

https://www.php.net/manual/en/function.sprintf.php

Basic concatenation of the string elements would work too $string1 . ' ' . $string2.

But not sure if you want the existing values as well.

What ever is receiving the data could also format it as a string

Brett
  • 1,951
  • 2
  • 28
  • 35
  • Please help to close basic duplicate questions in 2022. We already have at least 5 pages for all basic questions. The time has come to consolidate the content on Stack Overflow. – mickmackusa Jan 23 '22 at 22:09
  • i solved using $return_array['text'] = "($id) " . $this->data_from->format('Y-m-d') . " / " . $this->data_to->format('Y-m-d'); – Gene_Nostrada Jan 24 '22 at 10:34