What i try: I want to build a collection from a model that has multiple entries. The model has a relation (groups) where I don’t want to output a group (‘Lorem Ipsum’). Since the relation groups has many table columns and I only have id and name I map my result and output the two columns with only().
I try to add a condition to a function. I wonder if there is a function for the Laravel function filter()
where you can only get specific fields back. Like for example $someModel->only(['id', 'name'])
.
Currently I am solving it with a map()
after a filter()
$filtered = MainModel::with('relationA')->get()->map(fn (MainModel $mainModel $mainModel) => [
'id' => $mainModel->getRouteKey(),
'name' => $mainModel->name,
'relationAFiltered' => $mainModel->relationA->filter(fn (SomeModel $someModel) =>
$someModel->name !== 'Lorem Ipsum')->map(fn($g) => $g->only(['id', 'name'])),
2
Answers
I think what you are looking for is to only include certain columns of the relationship, but not to include anything unless it has a certain name?
To select which columns are included from the related model and apply a condition, the
with()
method can be used with a closure.Why are you converting your model to the array? Is it for the response?
If it’s for the response then you should use resources and in this case you can separate your code. For example:
Why resources are better than ordinary arrays:
If you do it not for the response and you just don’t like to use
map
+filter
several times in your code then you can put this logic to an independent method:Connect your collection with the model
Usage:
Your own collections have advantages and disadvantages:
Model::newCollection
for each modelI hope I understood correctly the question