You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9.x中API Resource的with方法失效问题

解决Laravel API Resource中with方法不生效的问题

我碰到过类似的情况!这是因为当你返回分页的资源集合时,Laravel会自动生成分页相关的meta数据,而默认情况下资源类的with方法返回的内容不会被自动合并到最终响应里——这也是为什么它在Laravel 8里能正常工作,现在却失效的原因(框架版本更新带来的响应处理逻辑变化)。

下面给你两种可行的解决方案:

方案一:用additional()方法追加自定义Meta

直接在返回资源集合的时候,通过additional()方法把你需要的columns元数据追加进去,这样就能和分页的meta合并了:

public function index() {
    $prescriptions = Prescription::paginate(5);
    return PrescriptionResource::collection($prescriptions)->additional([
        'meta' => [
            'columns' => [
                'id',
                'prescription',
                'quantity',
                'cost',
                'repeat',
                'company_id',
                'company',
                'location_id',
                'location',
                'manufacturer_id',
                'manufacturer',
                'practice_id',
                'practice',
                'prescriber_id',
                'prescriber',
                'created_at',
                'updated_at',
            ],
        ]
    ]);
}

方案二:自定义分页资源集合类

如果你希望把自定义Meta的逻辑封装到资源相关的类里,而不是写在控制器中,可以创建一个专门的集合类来处理:

  1. 新建PrescriptionCollection类:
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PrescriptionCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }

    /**
     * Get additional data that should be returned with the resource array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function with($request)
    {
        // 合并父类的分页meta和自定义的columns meta
        return array_merge(parent::with($request), [
            'meta' => [
                'columns' => [
                    'id',
                    'prescription',
                    'quantity',
                    'cost',
                    'repeat',
                    'company_id',
                    'company',
                    'location_id',
                    'location',
                    'manufacturer_id',
                    'manufacturer',
                    'practice_id',
                    'practice',
                    'prescriber_id',
                    'prescriber',
                    'created_at',
                    'updated_at',
                ],
            ]
        ]);
    }
}
  1. 修改控制器的index方法,返回这个自定义集合:
public function index() {
    $prescriptions = Prescription::paginate(5);
    return new PrescriptionCollection($prescriptions);
}

两种方案都能让你自定义的columns元数据出现在响应的meta里,你可以根据自己的代码风格选择其中一种。

内容的提问来源于stack exchange,提问作者Charlotte Wells

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 15:49:05