0

我在 laravel 5.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
    }
}
4

1 回答 1

1

在 viewCustomers() 函数中,创建一个包含成功所需的附加数据的数组:

$data = [
    'status' => true,
    'message' => 'All customers fetched successfully',
];

然后返回带有附加数据的 json 响应:

return CustomerResource::collection($customers)->additional($data);
于 2020-03-26T16:17:15.323 回答