Laravel : search or filter the collection
Tag : php , By : user119985
Date : March 29 2020, 07:55 AM
wish of those help I suspect it has to do with how you are using Eloquent. You can't simply apply methods to the object if it was created using the "new" keyword. $newBrands = new Brand;
// This won't work
$newBrands->where('is_active', '=', $status);
// This will work
$newBrands = $newBrands->where('is_active', '=', $status);
$newBrands = Brand::limit(100);
// This will work
$newBrands->where('is_active', '=', $status);
$newBrands = DB::table('brands');
// This wil work
$newBrands->where('is_active', '=', $status);
|
Search value deeply nested within a Laravel Collection
Date : March 29 2020, 07:55 AM
|
Laravel collection search case insensitive
Tag : php , By : user176445
Date : March 29 2020, 07:55 AM
this will help You could instead use the filter() method with a callback that does what you want: $collection = $collection->filter(function ($item) use ($attribute, $value) {
return strtolower($item[$attribute]) == strtolower($value);
});
|
Laravel Collection Search in View
Tag : php , By : user119413
Date : March 29 2020, 07:55 AM
should help you out Collections 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 , 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
search in collection laravel
Date : March 29 2020, 07:55 AM
wish helps you I have a very large collection of 150 objects , Firstwhere: $exists = $items->firstWhere(['emp_id' => 8, 'dst_id' => 8]) !== null;
$needle = 8;
$exists = $items->first(function($item) use($needle) {
return $item->emp_id === $needle && $item->dst_id === $needle ;
}) !== null;
$needle = 8;
$exists = $items->filter(function($item) use($needle) {
return $item->emp_id === $needle && $item->dst_id === $needle ;
})->count() > 0;
|
|