-4

I have a problem with this response

{
"success": {
    "id": 11,
    "title": "omar",
    "date": "2021-08-13",
    "start_time": "12:50:00",
    "end_time": "23:00:00",
    "end_date_time": "2021-08-13 23:00:00",
    "percentage": 10,
    "instruction": "asdadffa",
    "status": 1,
    "is_taken": 0,
    "is_closed": 0,
    "is_waiting": 0,
    "is_running": 0,
    "auto_mark": 0,
    "active_status": 1,
    "created_at": "2021-08-13T12:49:21.000000Z",
    "updated_at": "2021-08-13T12:49:43.000000Z",
    "class_id": 1,
    "section_id": 1,
    "subject_id": 2,
    "created_by": 1,
    "updated_by": 1,
    "school_id": 1,
    "academic_id": 1
}}

I want to remove the internal brackets {} of success a replace it with []

and here is my ideal response, something like that

{
"success": [
"id": 11,
"title": "omar",
"academic_id": 1
]}

thank you

11
  • try with toArray() collection method laravel.com/docs/8.x/collections#method-toarray Commented Aug 14, 2021 at 10:06
  • I've tried that too, no luck! Commented Aug 14, 2021 at 10:08
  • 1
    Removing the brackets {} would make it invalid JSON. Why would you want to do that? Commented Aug 14, 2021 at 10:20
  • @brombeer sorry I just forgot to remove the , the response was big os I cut it off I have updated my question Commented Aug 14, 2021 at 10:25
  • 2
    You are trying to make an array behave like an object without it actually being an object, so the exact structure is not possible. Commented Aug 14, 2021 at 11:37

3 Answers 3

1

the way, you want the response will make it an invalid json. since an array in json object can't contain key-value pair data. An array in json can have multiple json objects in it. You can have the response like this:

{
    "success": [
        {
            "id": 11,
            "title": "omar",
            "academic_id": 1
        }
    ]
}

To understand your need, can I please know why do you need that type of response? by the way, the response you are getting is a valid json response.

Sign up to request clarification or add additional context in comments.

2 Comments

the front end asked this format, and I couldn't do it! so I came here
Providing data in this format is not possible. So it would be better for you to contact with the front end. The first response in you question is a valid response and front end should do the work with that response.
0

try like this add toArray()

 $user= User::all()->toArray();
$post= Post::all()->toArray();
$comment= Comment:all()->toArray();

Response::json(['user'=>$user,'post'=>$post,'comment'=>$comment]);

3 Comments

thanks for answering, but it didn't work!
please explain more but maybe this link help you => stackoverflow.com/questions/59685133/…
or this one return Response::json($posts->toArray());
0

$user= User::all()->toArray();

return response()->json(['user'=>$user]);

2 Comments

thanks for answering, but it didn't work!
try this return response()->json([ 'user' => $user->values()->toArray() ]);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.