[英]Is there an easier way to filter columns in a Laravel Resource Collection?
<?php
namespace App\Http\Resources\Moderate;
use Illuminate\Http\Resources\Json\ResourceCollection;
class MoveSearchCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
foreach($this->collection as $key => $value) {
$this->collection[$key] = $this->collection[$key]->only(['id', 'name']);
}
return [
'data' => $this->collection,
];
}
}
我上面有集合,可以對其進行過濾,但是有沒有更簡單的方法來僅包含某些字段?
當然,不是收集模型,而是收集單個資源。 如果您在此處查看 ,它將向您展示如何首先制作單個資源,然后再與資源集合一起收集它。 在單個資源中,您可以指定任何列或這些列上的轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.