lundi 16 septembre 2019

add status and message keys to the resource json response in laravel

I am using a resource in laravel 5.5 from which I am returning the collection in my controller but I am unable to customize it's output json. I want to add status and message key to it's json output. I have tried to modify the toArray method but couldn't find a solution

Controller:

namespace App\Http\Controllers;

use App\cards,App\property_spaces,App\customers;
use App\Http\Resources\customers as CustomerResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ApiController extends Controller
{
public function viewCustomers()
{
      try
      {
        $customers = customers::paginate();
        return CustomerResource::collection($customers);
      }
      catch (\Throwable $e) {
        $arr = array(
          'status' => false,
          'message' => 'Problem with some code',
          'errorMessage' => $e->getMessage()
        );
        return response()->json($arr, 200);
      }
}
}

Resource:

class customers extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

Actual Result:

{
    "data": [
        {
            "CustomerId": 1,
            "SocialMediaId": "1234567",
            "PrefixId": 1,
            "FirstName": "ABC",
            "LastName": "ABC",
            "EmailAddress": "abc@hotmail.com",
            "MobileNo": null,
            "IsActive": 1,
            "CreatedAt": "2019-07-15 15:10:28",
            "DeviceId": "",
            "ProfileImage": "image1563196613907_6571.jpeg",
            "Lang": "ar"
        },
        {...},
        {...},
        {...}
    ],
    "links": {
        "first": "http://lsapp.uzair/api/allCustomers?page=1",
        "last": "http://lsapp.uzair/api/allCustomers?page=7",
        "prev": null,
        "next": "http://lsapp.uzair/api/allCustomers?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 7,
        "path": "http://lsapp.uzair/api/allCustomers",
        "per_page": 10,
        "to": 10,
        "total": 65
    }
}

Expected Result (Status and Message Key Added)

{
    "status": true,
    "message": "All customers fetched successfully",
    "data": [
        {
            "CustomerId": 1,
            "SocialMediaId": "1234567",
            "PrefixId": 1,
            "FirstName": "ABC",
            "LastName": "ABC",
            "EmailAddress": "abc@hotmail.com",
            "MobileNo": null,
            "IsActive": 1,
            "CreatedAt": "2019-07-15 15:10:28",
            "DeviceId": "",
            "ProfileImage": "image1563196613907_6571.jpeg",
            "Lang": "ar"
        },
        {...},
        {...},
        {...}
    ],
    "links": {
        "first": "http://lsapp.uzair/api/allCustomers?page=1",
        "last": "http://lsapp.uzair/api/allCustomers?page=7",
        "prev": null,
        "next": "http://lsapp.uzair/api/allCustomers?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 7,
        "path": "http://lsapp.uzair/api/allCustomers",
        "per_page": 10,
        "to": 10,
        "total": 65
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire