如何通过数组集合进行搜索过滤?
How to do search filtering through a collection of arrays?
我打算在laravel 5中构建高级搜索功能。我通过过滤一些字段(例如 negeriID、categoryID 和 operasiID)从 'itemregistrations' table 查询。我需要做数组映射来计算每个项目的年龄值并放入数组中。通过使用 itemregistration table 获取值并计算年龄运行正常,但在搜索 if 语句时出现问题。它无法通过集合中的数组搜索和检索值。
$newitem = DB::table('itemregistrations')
->select('itemregistrations.*')
->get();
//added code to get 'age' value:
$newitem->map(function ($detail) {
$detail->age = \Carbon\Carbon::createFromFormat('Y',$detail->lahir_yy)->diffInYears();
return $detail;
});
if ($request->has('negeri_lahir')) {
$newitem->where('NegeriID', '==', $request->negeri_lahir);
}
if ($request->has('kategori')) {
$newitem->where('CategoryID', $request->kategori);
}
if ($request->has('pangkat')) {
$newitem->where('OperasiID', $request->pangkat);
}
dd($newitem->get());
问题是因为添加了数组映射,将集合转换为数组值导致此错误。
它正在产生错误:
Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in C:\xampp\htdocs\
这是 dd($newitem) 集合中的数组列表;
#items: array:1123 [▼
0 => {#709 ▶}
1 => {#680 ▶}
2 => {#681 ▶}
3 => {#712 ▶}
Collection {#671 ▼
#items: array:1123 [▼
0 => {#709 ▼
+"ItemRegistrationID": 1
+"COID": 109064
+"FType": ""
+"STNo": "0"
+"RegistrationDate": "2005-12-01"
and more attributes...
如何启用数组列表搜索?
- 首先,你不需要在查询中使用
select()
。
- 使用
when()
在数据库查询中进行过滤看起来更好。
尝试:
$newitem = DB::table('itemregistrations')
->when(request('age'), function($query){
$query->whereRaw('YEAR(curdate()) - lahir_yy >= ?', [request('age')]);
})
->when(request('negeri_lahir'), function($query){
$query->where('NegeriID', request('negeri_lahir'));
})
->when(request('kategori'), function($query){
$query->where('CategoryID', request('kategori'));
})
->when(request('pangkat'), function($query){
$query->where('OperasiID', request('pangkat'));
})
->get();
我打算在laravel 5中构建高级搜索功能。我通过过滤一些字段(例如 negeriID、categoryID 和 operasiID)从 'itemregistrations' table 查询。我需要做数组映射来计算每个项目的年龄值并放入数组中。通过使用 itemregistration table 获取值并计算年龄运行正常,但在搜索 if 语句时出现问题。它无法通过集合中的数组搜索和检索值。
$newitem = DB::table('itemregistrations')
->select('itemregistrations.*')
->get();
//added code to get 'age' value:
$newitem->map(function ($detail) {
$detail->age = \Carbon\Carbon::createFromFormat('Y',$detail->lahir_yy)->diffInYears();
return $detail;
});
if ($request->has('negeri_lahir')) {
$newitem->where('NegeriID', '==', $request->negeri_lahir);
}
if ($request->has('kategori')) {
$newitem->where('CategoryID', $request->kategori);
}
if ($request->has('pangkat')) {
$newitem->where('OperasiID', $request->pangkat);
}
dd($newitem->get());
问题是因为添加了数组映射,将集合转换为数组值导致此错误。 它正在产生错误:
Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in C:\xampp\htdocs\
这是 dd($newitem) 集合中的数组列表;
#items: array:1123 [▼
0 => {#709 ▶}
1 => {#680 ▶}
2 => {#681 ▶}
3 => {#712 ▶}
Collection {#671 ▼
#items: array:1123 [▼
0 => {#709 ▼
+"ItemRegistrationID": 1
+"COID": 109064
+"FType": ""
+"STNo": "0"
+"RegistrationDate": "2005-12-01"
and more attributes...
如何启用数组列表搜索?
- 首先,你不需要在查询中使用
select()
。 - 使用
when()
在数据库查询中进行过滤看起来更好。
尝试:
$newitem = DB::table('itemregistrations')
->when(request('age'), function($query){
$query->whereRaw('YEAR(curdate()) - lahir_yy >= ?', [request('age')]);
})
->when(request('negeri_lahir'), function($query){
$query->where('NegeriID', request('negeri_lahir'));
})
->when(request('kategori'), function($query){
$query->where('CategoryID', request('kategori'));
})
->when(request('pangkat'), function($query){
$query->where('OperasiID', request('pangkat'));
})
->get();