skip to Main Content

Hello im making a GET api in laravel that shows values from database and im trying to make it that the returned json result to have a header tag an example is this public api

https://dummy.restapiexample.com/api/v1/employees

As you can see this api shows the status:success and then data: , this is my code:

 function getDevice()
    {
        return Client::all();
    }

And this is the result i get in postman:

  data [  //here to have a header
        {
            "id": 1,
            "name": "Mr. Omari Schaefer DVM",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "29",
            "salary": "150",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 2,
            "name": "Hattie Brakus",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "30",
            "salary": "565",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 3,
            "name": "Sabrina Rosenbaum",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "31",
            "salary": "254",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 4,
            "name": "Ms. Fiona Fritsch",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "32",
            "salary": "029",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 5,
            "name": "Michael Dooley",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "33",
            "salary": "265",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 6,
            "name": "Prof. Kelley Koepp Jr.",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "34",
            "salary": "688",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 7,
            "name": "Herminia McClure PhD",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "35",
            "salary": "103",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 8,
            "name": "Morton Considine",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "36",
            "salary": "804",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 9,
            "name": "Katlyn Muller",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "37",
            "salary": "695",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 10,
            "name": "Wilber Stehr",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "38",
            "salary": "941",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 11,
            "name": "Test User",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "60",
            "salary": "448",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        }
    ]

What im trying to do is:

    [
        {
            "id": 1,
            "name": "Mr. Omari Schaefer DVM",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "29",
            "salary": "150",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 2,
            "name": "Hattie Brakus",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "30",
            "salary": "565",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 3,
            "name": "Sabrina Rosenbaum",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "31",
            "salary": "254",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 4,
            "name": "Ms. Fiona Fritsch",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "32",
            "salary": "029",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 5,
            "name": "Michael Dooley",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "33",
            "salary": "265",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 6,
            "name": "Prof. Kelley Koepp Jr.",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "34",
            "salary": "688",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 7,
            "name": "Herminia McClure PhD",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "35",
            "salary": "103",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 8,
            "name": "Morton Considine",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "36",
            "salary": "804",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 9,
            "name": "Katlyn Muller",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "37",
            "salary": "695",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 10,
            "name": "Wilber Stehr",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "38",
            "salary": "941",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        },
        {
            "id": 11,
            "name": "Test User",
            "email": "[email protected]",
            "phone_number": "0697830800",
            "age": "60",
            "salary": "448",
            "created_at": "2023-02-17T08:36:00.000000Z",
            "updated_at": "2023-02-17T08:36:00.000000Z"
        }
    ]

2

Answers


  1. function getDevice() 
    {
     return ['data' => 
     Client::all(),
     };
    
    Login or Signup to reply.
  2. Please refer to the below solution for every response :

    return response()->json(‘status’ => ‘success’,’message’ => ‘Client listing successfully.’,’data’ => Client::all());

    If you know how to use the helper function in Laravel then Please used the below for all responses:

    Example :

    In Controller, Just add the below line :

    return  apiResponse('success','Client listed successfully',Client::all()]);
    if (!function_exists('apiResponse')) {
        function apiResponse($status = null, $message = null, $data = null, $code = 200, $extra = []) {
            $response = [
                'status' => $status,
                'message' => $message,
                'data' => $data,
            ];
            if(!empty($extra)){
                $response = array_merge($response,$extra);
            }
            return response()->json($response, $code);
        }
    }

    I think this will help you a lot.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search