Filtering Eloquent collection data with $collection->filter()

LaravelLaravel 4Eloquent

Laravel Problem Overview


I'm trying to filter the following collection using the collection filter() method:

$collection = Word::all();

where the JSON output looks like this:

[{"id": "1","word": "dog","phonetic": "dog","mean": "pies","assoc": "some example text","author_id": "3","user_id": "3"},{"id": "2","word": "sun","phonetic": "sun","mean": "słońce","assoc": "lorem ipsun dolor sit amet","author_id": "3","user_id": "2"}, ...]

However, when filtering the collection:

$filtered_collection = $collection->filter(function($item)
    {
        if($item->isDog())
        {
            return $item;
        }
 });

The filtered collection JSON output will look like this:

 {"1":
 {
 "id": "1",
 "word": "dog",
 "phonetic": "dog",
 "mean": "pies",
 "assoc": "some example text",
 "author_id": "3",
 "user_id": "3"
 },
 "2":
 {
 "id": "2",
 "word": "sun",
 "phonetic": "sun",
 "mean": "słońce",
 "assoc": "lorem ipsun dolor sit amet",
 "author_id": "3",
 "user_id": "2"
 }}

How can I keep the original JSON output when filtering a collection? I'd like to have an array of my Eloquent model instances when filtering the original collection . Thanks in advance :)

Laravel Solutions


Solution 1 - Laravel

The collection's filter method calls array_filter on the underlying array, which, according to the PHP docs, preserves the array keys. This then results in your array being converted to a JavaScript object instead of an array.

Call values() on your collection to reset the keys on the underlying array:

$filtered_collection = $collection->filter(function ($item) {
    return $item->isDog();
})->values();

Side note: in newer versions of Laravel, you can use a higher order message to shorten the above into this:

$filtered_collection = $collection->filter->isDog()->values();

Solution 2 - Laravel

Just convert it to JSON with keeping in mind what the Laravel documentation states:

> Note: When filtering a collection and converting it to JSON, try calling the values function first to reset the array's keys.

So the final code would be:

$filtered_collection->values()->toJson();

Solution 3 - Laravel

Filtering from database could be done in this method also.

 //request the form value 
  $name=$request->name;
  $age=$request->age;
  $number=$request->phone;

 //write a query to filter
 $filter_result = DB::table('table_name')

 ->where('name', 'like', '%'.$name.'%')
 ->orWhere('age', 'like', '%'.$age.'%')
 ->orWhere('phone', 'like', '%'.$number.'%')

 ->get();

 if(is_null($filter_result)){
 return redirect()->back()->with('message',"No Data Found");

}else{
      return view('resultpage',compact('filter_result'));
}

Solution 4 - Laravel

If you have eloquent collection array and you want to filter it from some attribute, you can try following filter to achieve this.

In my case, $reports is variable contain eloquent collection object. and i want to filter from this collection with id attribute.

$filter_id = $reports->filter(function ($item) {
   return $item->id==5;
})->first();

now you have $filter_id variable that contains object with id=5.

you can access all attribute of id=5, like $filter_id->attribute_name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTenzoruView Question on Stackoverflow
Solution 1 - LaravelJoseph SilberView Answer on Stackoverflow
Solution 2 - LaraveltotymedliView Answer on Stackoverflow
Solution 3 - LaravelKAUSHAL MAURYAView Answer on Stackoverflow
Solution 4 - LaravelYuvraj HingerView Answer on Stackoverflow