Reputation: 2233
I want to search from a collection with multiple Value in View page
My collection will have
- product_id
- quantity
- color_id
- size_id
- sold_price
I want to search from this collection with color_id
, size_id
and product_id
in view page so the other values like quantity
and sold_price
with respective collection will print inside for loop. Is it possible?
@foreach($colors as $color)
<tr>
<td>{{$color->color_name}}</td>
@foreach($sizes as $size)
<td>
// Here Collection search will be implemented with color_id, size_id
// and product_id and print respective product price and quantity
</td>
@endforeach
</tr>
@endforeach
Upvotes: 1
Views: 56
Reputation: 29258
Collection
s have a filter()
method that iterates over the entries in the Collection
and returns a subset of records that match a given condition. You can use this in a view with the @php ... @endphp
directive, then loop over the returned Collection
and output columns (might make more sense to do new <tr>
, but that's just a display thing):
@foreach($colors as $color)
<tr>
<td>{{$color->color_name}}</td>
@foreach($sizes as $size)
@php
$entries = $collection->filter(function($record) use($product, $color, $size){
return $record->color_id == $color->id && $record->size_id == $size->id && $record->product_id == $product->id;
});
@endphp
@foreach($entries AS $entry)
<td>{{ $entry->quantity }}</td>
<td>{{ $entry->sold_price }}</td>
@endforeach
@endforeach
</tr>
@endforeach
Note: To ensure you can check $color->id
, $size->id
and $product->id
, make sure to pass them to the function using use($product, $color, $size)
.
Upvotes: 2
Reputation: 1
If its Collection, you can use filter method
$collection->filter(function ($value, $key) use ($searchValue) {
return $color_id === $searchValue;
});
Upvotes: 0