0

I am new to laravel and php,

I have the code below, REST API

public function sendMessage(Request $request)
    {
        $message = Message::create($request->all());
        return response()->json(['result'=>'success','created_at'=>$message->created_at],200);
    }

This is displayed like this in the postman

{
    "result": "success",
    "created_at": "2020-06-29T23:31:32.000000Z"
}

If I change the return message-object by changing return statement to

return response()->json(['result'=>'success','created_at'=>$message],200);

Then time format is displayed differently as below

{
    "result": "success",
    "created_at": {
        "sender": "47",
        "receiver": "23",
        "message": "hello world reply",
        "updated_at": "2020-06-29 23:38:53",
        "created_at": "2020-06-29 23:38:53",
        "id": 515
    }
}

I do not want this form "2020-06-29T23:31:32.000000Z" when I access it as a property,not sure what is this 00000Z at then end. want it like this "2020-06-29 23:38:53" Any help

Ahmed
  • 14,503
  • 22
  • 92
  • 150
  • There are many many questions and answers here about formatting `created_at` ... did you check those, do they not help? – Don't Panic Jun 29 '20 at 23:52
  • Does this answer your question? [How to change default format at created\_at and updated\_at value laravel](https://stackoverflow.com/questions/24441395/how-to-change-default-format-at-created-at-and-updated-at-value-laravel) – Don't Panic Jun 29 '20 at 23:52
  • That question did not help that is a format change question, in my case it is a display problem apparently using carbon instance as stated in the answers below. – Ahmed Jun 30 '20 at 00:55
  • The answers below describe the "why" of what you are seeing. The question/answers I linked to describe the "how" of doing what you asked: `I do not want this form ... want it like this`. Specifically, look at the answer that describes `$dateFormat`. https://laravel.com/docs/7.x/eloquent-mutators#date-mutators – Don't Panic Jun 30 '20 at 01:13
  • Incorrect, answer of lagbox below provides an easy solut – Ahmed Jun 30 '20 at 01:26

2 Answers2

0

When you access the data member ->created_at you get a Carbon instance, instead if you serialize the object, you get its attribute.

This is why you are getting two different serialization, because you are serializing two different things (one is a Carbon instance, the onthe one is a Model)

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • "serialize the object" with regards to this I need to see details, can you refer an article or documentation ? I am new to PHP, – Ahmed Jun 30 '20 at 00:52
  • @Ahmed here https://www.php.net/manual/en/function.json-encode.php you can read about the serialization (in json) – Alberto Sinigaglia Jun 30 '20 at 09:36
0

If you convert the Carbon instance to a string it will be in the same format as you see in the serialized model. When you pass it without doing this it is going to json_encode the value which will return it how you are currently seeing it.

return response()->json([
    'result' => 'success',
    'created_at' => (string) $message->created_at
],200);
lagbox
  • 48,571
  • 8
  • 72
  • 83
  • So if understand you correctly, casting it to string is like calling toString method on it. is json_encode not supposed to call toString/or similar method on the created_at object ? because json is ultimately a string ? – Ahmed Jun 30 '20 at 00:42