laravel 使用 $collection->filter() 过滤 Eloquent 集合数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21974402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filtering Eloquent collection data with $collection->filter()
提问by Tenzoru
I'm trying to filter the following collection using the collection filter() method:
我正在尝试使用集合 filter() 方法过滤以下集合:
$collection = Word::all();
where the JSON output looks like this:
JSON 输出如下所示:
[
{
"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:
过滤后的集合 JSON 输出将如下所示:
{"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 :)
过滤集合时如何保留原始 JSON 输出?在过滤原始集合时,我想拥有一组我的 Eloquent 模型实例。提前致谢 :)
回答by Joseph Silber
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.
集合的filter
方法调用array_filter
底层数组,根据 PHP 文档,它保留数组键。这会导致您的数组被转换为 JavaScript 对象而不是数组。
Call values()
on your collection to reset the keys on the underlying array:
调用values()
您的集合以重置底层数组上的键:
$filtered_collection = $collection->filter(function ($item) {
return $item->isDog();
})->values();
回答by totymedli
Just convert it to JSON with keeping in mind what the Laravel documentationstates:
只需将其转换为 JSON 并记住Laravel 文档中的说明:
Note:When filtering a collection and converting it to JSON, try calling the values function first to reset the array's keys.
注意:过滤集合并将其转换为 JSON 时,请先尝试调用 values 函数以重置数组的键。
So the final code would be:
所以最终的代码是:
$filtered_collection->values()->toJson();
回答by KAUSHAL MAURYA
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'));
}