php - Laravel 8: How can I pass an additional parameter from controller to collection resource
Get the solution ↓↓↓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 returningnull
Answer
Solution:
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'
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot access offset of type string on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.