Laravel 5 向laravel中的资源json响应添加状态键和消息键

Laravel 5 向laravel中的资源json响应添加状态键和消息键,laravel-5,laravel-response,laravel-resource,Laravel 5,Laravel Response,Laravel Resource,我正在使用Laravel5.5中的一个资源,从中返回控制器中的集合,但我无法自定义它的输出json。我想将状态和消息键添加到它的json输出中。我试图修改toArray方法,但找不到解决方案 控制器: namespace App\Http\Controllers; use App\cards,App\property_spaces,App\customers; use App\Http\Resources\customers as CustomerResource; use Illuminat

我正在使用Laravel5.5中的一个资源,从中返回控制器中的集合,但我无法自定义它的输出json。我想将状态和消息键添加到它的json输出中。我试图修改toArray方法,但找不到解决方案

控制器:

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);
      }
}
}
资源:

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

{
    "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
    }
}
已添加预期结果状态和消息键

{
    "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
    }
}

在viewCustomers函数中,使用成功时所需的其他数据创建一个数组:

$data = [
    'status' => true,
    'message' => 'All customers fetched successfully',
];
然后返回包含其他数据的json响应:

return CustomerResource::collection($customers)->additional($data);