In laravel 7 app I use collections in controller:
return (new TaskCollection($tasks));
and app/Http/Resources/TaskCollection.php :
<?php
namespace App\Http\Resources;
use App\Facades\MyFuncsClass;
use App\Task;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TaskCollection extends ResourceCollection
{
public static $wrap = 'tasks';
public function toArray($request)
{
return $this->collectResource($this->collection)->transform(static function (Task $task){
return [
'id' => $task->id,
....
'created_at' => $task->created_at,
'updated_at' => $task->updated_at,
'deleted_at' => $task->deleted_at,
];
});
}
public function with($request)
{
return [
'meta' => [
...
]
];
}
}
I need to add order field(from 0 value) + 1 on next row. How can I do it?
Thanks!
Use
map
function: