Answer the question
In order to leave comments, you need to log in
How to pass variable to Laravel Resource Collection?
In the controller I do a check and give the collection in json
$check = проверка с запросом в бд возвращает true/false;
return ProjectResource::collection($projects);
'check' => $this->metod($check),
Answer the question
In order to leave comments, you need to log in
To the base class of the collection
/**
* Create new anonymous resource collection.
*
* @param mixed $resource
* @param callable|null $each
*
* @return AnonymousResourceCollection
*/
public static function collection($resource, callable $each = null)
{
$collection = new AnonymousResourceCollection($resource, \get_called_class());
if ($resource && (! $resource instanceof MissingValue) && $each) {
$collection->resource->each($each);
}
return $collection;
}
$check = true;
return ProjectResource::collection($projects, function (ProjectResource $resource) use ($check) {
$resource->setCheck($check);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question