我正在尝试在Laravel中实现高级搜索,但存在一个问题,问题是我想筛选多个表以获取记录并将它们存储在某个数组中以进行显示。我需要显示一系列数据,以显示其格式错误,我需要根据Laravel分页对其进行格式化。
这是我正在尝试的代码。
public static function getHeaderSearchedResults($term) {
$resultArray = array();
$searchable_ids = array();
$subjects = SheetSubject::where('subject_title', 'LIKE', '%' . $term . '%')->get(['subject_id'])->toArray();
$units = SheetUnit::where('unit_title', 'LIKE', '%' . $term . '%')->get(['id'])->toArray();
$years = SheetYear::where('year_title', 'LIKE', '%' . $term . '%')->get(['year_id'])->toArray();
$sheets = Sheet::where('sheet_title', 'LIKE', '%' . $term . '%')->get(['sheet_id'])->toArray();
$finalArray = array_merge($subjects, $units, $years, $sheets);
foreach ($finalArray as $ids) {
foreach ($ids as $id) {
array_push($searchable_ids, $id);
}
}
dump($searchable_ids);
foreach ($searchable_ids as $new_id) {
$sheet = Sheet::where('sheet_subject_id', $new_id)
->orwhere('sheet_unit_id', $new_id)
->orwhere('sheet_year_id', $new_id)
->orwhere('sheet_id', $new_id)
->orwhere('sheet_s_id', $new_id)
->orderBy('sheet_title', 'asc')->paginate(12);
dump($new_id);
array_push($resultArray, $sheet);
}
return $resultArray;
}
我得到的答复是:
array:15 [▼
0 => LengthAwarePaginator {#273 ▶}
1 => LengthAwarePaginator {#278 ▶}
2 => LengthAwarePaginator {#251 ▶}
3 => LengthAwarePaginator {#294 ▶}
4 => LengthAwarePaginator {#308 ▶}
5 => LengthAwarePaginator {#322 ▼
#total: 35
#lastPage: 3
#items: Collection {#351 ▼
#items: array:12 [▶]
}
#perPage: 12
#currentPage: 1
#path: "http://localhost:8000/primary-resources"
#query: []
#fragment: null
#pageName: "page"
}
6 => LengthAwarePaginator {#275 ▶}
7 => LengthAwarePaginator {#364 ▶}
8 => LengthAwarePaginator {#367 ▶}
9 => LengthAwarePaginator {#370 ▶}
10 => LengthAwarePaginator {#373 ▶}
11 => LengthAwarePaginator {#376 ▶}
12 => LengthAwarePaginator {#379 ▶}
13 => LengthAwarePaginator {#382 ▶}
14 => LengthAwarePaginator {#385 ▼
#total: 1
#lastPage: 1
#items: Collection {#377 ▶}
#perPage: 12
#currentPage: 1
#path: "http://localhost:8000/primary-resources"
#query: []
#fragment: null
#pageName: "page"
}
]
我想要的回复是这样的:
LengthAwarePaginator {#248 ▼
#total: 5548
#lastPage: 463
#items: Collection {#257 ▼
#items: array:12 [▼
0 => Sheet {#258 ▶}
1 => Sheet {#259 ▶}
2 => Sheet {#260 ▶}
3 => Sheet {#261 ▶}
4 => Sheet {#262 ▶}
5 => Sheet {#263 ▶}
6 => Sheet {#264 ▶}
7 => Sheet {#265 ▶}
8 => Sheet {#266 ▶}
9 => Sheet {#267 ▶}
10 => Sheet {#268 ▶}
11 => Sheet {#269 ▶}
]
}
#perPage: "12"
#currentPage: 1
#path: "http://localhost:8000/primary-resources"
#query: []
#fragment: null
#pageName: "page"
}
任何人都知道我做错了什么。将不胜感激。预先谢谢你。