【问题标题】:add status and message keys to the resource json response in laravel向 laravel 中的资源 json 响应添加状态和消息键
【发布时间】: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
    }
}

【问题讨论】:

    标签: laravel-5 laravel-response laravel-resource


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 2017-04-26
      相关资源
      最近更新 更多