I have implemented Sphinx Search in Laravel 5. Now I want to implement pagination on this. I didn't get any help and Laravel 5 paginate is not working on Sphinx Search.
Here is my code:
public function index() {
$sphinx = new SphinxSearch();
$results = $sphinx->search('php','jobma1')->query()->paginate(5); //geting error :(
$results = $sphinx->search('php','jobma1')->query(); //working fine :)
dd($results); die;
}
Please let me know how to implement paginate on Sphinx Search in Laravel 5
use this
In a controller
$page = 1
$queryString = $request->all();
if (isset($queryString['page'])) { $page = $queryString['page']; }
if ($page > 0) { $startFrom = ($page-1) * $numRecPerPage; }
$startFrom = 0;
$numRecPerPage = 6;
$sphinx = new SphinxSearch();
$sphinx->setMatchMode('SPH_MATCH_BOOLEAN');
$sphinx->search('php Developer software ', 'jobma1');
// Limit parameters (Number of records display, start from )
// If first parameter 10 then 10 records will display
// If second parameter is 5 then first 5 records will not display = display from 6
$result = $sphinx->limit($numRecPerPage, $startFrom)->query();
In a View
<?php //dd($result);?>
<?php if (isset($result['matches'])) { ?>
<table>
<tr style="text-align:left">
<th style= "width:100px;">
ID
<th>
<th>
Post Name
</th>
</tr>
<tbody>
<?php
foreach ($result['matches'] as $key => $value) {
echo '<tr>';
echo '<td>'; echo $key; echo '<td>';
echo '<td>'; echo $value['attrs']['jobma_post_name']; echo '<td>';
echo '<tr>';
}
?>
</tbody>
</table>
<?php
// Pagination Logic start from here
$totalRecords = $result['total_found'];
if ($totalRecords > $numRecPerPage) {
$totalPages = ceil($totalRecords / $numRecPerPage);
$startLoop = 1;
$endLoop = $totalPages;
if ( $totalPages > 6) {
$endLoop = 6;
}
$page = $_GET['page'];
$endPage = $page+1;
if ($page >= 4) {
$startLoop = $page - 3;
$endLoop = $page + 3;
if ($endLoop > $totalPages) {
$startLoop = $totalPages - 6;
$endLoop = $totalPages;
}
if ($startLoop < 1) {
$startLoop = 1;
}
}
if ($page > 1) {
$prePage = $page - 1;
echo "<a href='users?page=1'>".'<<'."</a> ";
echo "<a href='users?page=$prePage'>".'<'."</a> ";
}
for ($i=$startLoop; $i<=$endLoop; $i++) {
$class ="";
if ($i == $page) { $class ="class='activeClass'"; }
if ($page == $i ) { echo "<a href='javascript:void(0);' $class> ".$i; } else { echo "<a href='users?page=".$i."' $class> ".$i; }
if ($i < $endLoop) { echo " </a> "; } else { echo "</a>"; }
}
if ($endPage <= $totalPages ) {
echo "<a href='users?page=$endPage'>".'>'."</a> "; // Goto last page
echo "<a href='users?page=$totalPages'>".'>>'."</a> ";
}
echo '<br/>';
echo '<br/>';
}
echo 'Total Number of Records: '. $totalRecords;
// Pagination Logic end from here
?>
<style>
a { background: none repeat scroll 0 0 #FFD700; border: 1px solid #FFD700; margin: 0 3px 2px; padding: 2px 3px 0; text-decoration: none; }
a.activeClass { color:green; font-weight: bolder; }
</style>
<?php } else { ?>
No records found
<?php } ?>