1

I want to pass an additional parameter from controller to collection resource, Please have a look at the following code, I don't know why It's not working. I might be doing something wron here, please help

Controller

$batchStudents = $batch->students->except($batch->host->id);
$batchStudents->test = 'This is testing';
$students = UserCollection::collection($batchStudents);

Collection:

    class UserCollection extends JsonResource {
    return [
            [...]
            'test' => $this->test,
        ];
    }

Current Result: Cuurently its returning null

Muhammad Owais
  • 980
  • 3
  • 17
  • 37
  • Does this answer your question? [Laravel 5.6 - Pass additional parameters to API Resource?](https://stackoverflow.com/questions/50638257/laravel-5-6-pass-additional-parameters-to-api-resource) – porloscerros Ψ Jul 31 '21 at 16:36
  • Maybe you are looking for https://laravel.com/docs/8.x/eloquent-resources#adding-meta-data – Dimitri Mostrey Jul 31 '21 at 20:11

1 Answers1

-1

This is the solution if you want to add additional information (not for the model)

Controller:

$batchStudents = $batch->students->except($batch->host->id);
$$param = ['test'=>'This is testing'];
return UserCollection::collection($batchStudents)->additional($param);

Collection:

class UserCollection extends JsonResource {
    return [
            [...]
            //'test' => $this->test,
        ];
}

return

{
  data:data Array[...]
  ...
  test: 'This is testing'
}
kindy ww
  • 9
  • 2