Пагинация Laravel работает только с параметрами получения.
и после разбиения на страницы преобразование массива принимает разные концепции
следующий пример иллюстрирует эту концепцию
1. Get data from database
2. Filter data corresponding search_content using in_array (in_array accepts data array only and not for objects)
3. Perform manual pagination and display records
Код для RecordsController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use App\Records;
class RecordController extends Controller
{
// Show Entry Form
public function index()
{
return view('search_entry');
}
public function search(Request $request)
{
$search = Input::get('searchcontent');
$results=Records::get()->toArray();
$records=[];
foreach($results as $result) //in_array accepts array only
{
if(in_array($search, $result))
{
array_push($records, $result);
}
}
$page = $request->page; // current page for pagination
// manually slice array of product to display on page
$perPage = 5;
$offset = ($page-1) * $perPage;
$new_record = array_slice($records, $offset, $perPage);
// your pagination
$new_record = new Paginator($new_record, count($records), $perPage, $page, ['path' => $request->url(),'query' => $request->query()]);
return view('home',['records' => $new_record]);
}
}
Код для search_entry.blade.php
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LaravelTest</title>
</head>
<body>
<form action="{{url('search')}}" >
<h2>Please enter the keywords you want to search:</h2>
<input id="search"type=text name="searchcontent">
<input type="submit" value='Search'>
</form>
</body>
</html>
код для home.blade.php (результирующий просмотр php)
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LaravelTest</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
</head>
<body>
<h3>SEARCH CONTENT</h3>
<table border=1 cellspacing=0 cellpadding=2>
<thead>
<tr>
<td>ID</td> <td>NAME</td> <td>AGE</td>
</tr>
</thead>
<tbody>
<?php
foreach($records as $record)
{
echo "<tr><td>".$record['id']."</td><td>".$record['name']."</td><td>".$record['age']."</td></tr>";
}
?>
</tbody>
</table>
{{ $records->links('pagination::bootstrap-4') }}
</body>
</html>
Добавитьследующий код маршрутизации в Web.php
Route::get('search_entry','RecordController@index');
Route::get('search','RecordController@search');