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

Posted

技术标签:

【中文标题】向 laravel 中的资源 json 响应添加状态和消息键【英文标题】:add status and message keys to the resource json response in laravel 【发布时间】:2020-01-17 05:35:55 【问题描述】:

我在 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
    

【问题讨论】:

【参考方案1】:

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

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

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

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

【讨论】:

以上是关于向 laravel 中的资源 json 响应添加状态和消息键的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 向 json 列中的数组添加一个元素

从 Android 向 Laravel 发送 JSON

Laravel 中的 JSON 响应

如何在 Laravel 5.6 中向资源控制器添加自定义方法

Laravel 4 返回 json 作为响应

将json响应添加到flutter中的列表