Home > Enterprise >  when i am returning data getting data in data.why?
when i am returning data getting data in data.why?

Time:07-02

this is my return method in trait.how can i get only data without data in data. below is my postman response.

protected function success($message, $data , $status = Response::HTTP_OK)
{
    return $data 
        ? response()->json([
            'status' => 'success',
            'message' => $message,
            'data' => $data,
        ], $status) 
        : response()->json([
            'status' => 'success',
            'message' => $message,
        ], $status);
}

how can i get only data without data in data. below is my postman response.

{
    "status": "success",
    "message": "Fetched customer details",
    "data": {
        "current_page": 1,
        "data": [
            {
                "pid": "hkEH97ur",
                "name": "badri44",
                "email": "[email protected]",
                "mobile": "761487",
                "total_amount": 0,
                "used_amount": 0,
                "remaining_amount": 0,
                "active_status": "Active"
            },
            {
                "pid": "5j9vCsRb",
                "name": "badri3",
                "email": "[email protected]",
                "mobile": "9989893890808118",
                "total_amount": 0,
                "used_amount": 0,
                "remaining_amount": 0,
                "active_status": "Active"
            }
        ],
        "first_page_url": "http://localhost/retail/v1/customers?page=1",
        "from": 1,
        "next_page_url": null,
        "path": "http://localhost/retail/v1/customers",
        "per_page": 15,
        "prev_page_url": null,
        "to": 2
    }
}

CodePudding user response:

Assuming $data is array.

protected function success($message, $data , $status = Response::HTTP_OK)
{
       return $data ? response()->json([
        'status' => 'success',
        'message' => $message,
        'data' => $data['data'],
    ], $status) : response()->json([
        'status' => 'success',
        'message' => $message,
    ], $status);
}

If $data is object

protected function success($message, $data , $status = Response::HTTP_OK)
{
       return $data ? response()->json([
        'status' => 'success',
        'message' => $message,
        'data' => $data->data,
    ], $status) : response()->json([
        'status' => 'success',
        'message' => $message,
    ], $status);
}

CodePudding user response:

I am assuming $data is a paginator instance since it fits what is shown here which is an arrayable instance. In that case you would need to do the following:

protected function success($message, $data , $status = Response::HTTP_OK)
{
    return $data && $data instanceof Arrayable
        ? response()->json(array_merge([
            'status' => 'success',
            'message' => $message,
        ], $data->toArray()), $status) 
        : response()->json([
            'status' => 'success',
            'message' => $message,
        ], $status);
}

Note: Seeing two answers both starting with "Assuming" might indicate your question did a bad job at being accurate with the problem description.

CodePudding user response:

You could use

protected function success($message, $data, $status = Response::HTTP_OK)
{
    return response()->json(array_merge([
        'status' => 'success',
        'message' => $message,
    ], $data->toArray()), $status);
}

And you will get a response like

{
    "status": "success",
    "message": "Fetched customer details",
    "current_page": 1,
    "data": [
        {
            "pid": "hkEH97ur",
            "name": "badri44",
            "email": "[email protected]",
            "mobile": "761487",
            "total_amount": 0,
            "used_amount": 0,
            "remaining_amount": 0,
            "active_status": "Active"
        },
        {
            "pid": "5j9vCsRb",
            "name": "badri3",
            "email": "[email protected]",
            "mobile": "9989893890808118",
            "total_amount": 0,
            "used_amount": 0,
            "remaining_amount": 0,
            "active_status": "Active"
        }
    ],
    "first_page_url": "http://localhost/retail/v1/customers?page=1",
    "from": 1,
    "next_page_url": null,
    "path": "http://localhost/retail/v1/customers",
    "per_page": 15,
    "prev_page_url": null,
    "to": 2,
}

CodePudding user response:

You can also go with this approach

$response = $data->toArray();

$response['status'] = 'success';

$response['message'] = $message;

return response()->json($response, $status); 
  • Related