0

我在渲染方法中的 livewire 中有一个组件我调用 JSON 资源来映射数据并返回所有数据而不映射我在没有 livewire 的情况下测试相同的代码并且它可以工作

  public function render()
    {
        $allData = showReportResource::collection(Report::paginate(10));
        dd($allData);
        return view('livewire.all-component',compact('allData'));
    }

和我的资源代码

  class showReportResource extends JsonResource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'=>$this->id,
            'custom'=>'custom'
        ];
    }
}

和回报

0 => App\Http\Resources\showReportResource {#1530 ▼
    +resource: App\Models\Report {#1501 ▼
      #fillable: array:4 [▶]
      #connection: "mysql"
      #table: "reports"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [▼
        "id" => 1
        "reportable_id" => 1
        "reportable_type" => "App\Models\User"
        "content" => "report content is empty"
        "user_id" => 11
        "created_at" => "2021-09-06 11:36:08"
        "updated_at" => "2021-09-06 11:36:08"
      ]
      #original: array:7 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    +with: []
    +additional: []
  }

问题资源返回所有数据而没有看到资源如何解决

4

0 回答 0