Search/Pagination
Hello, I am working on a simple page to show search results in a table format from a database. I have managed to get the search function working correctly and used Laravel's built in Pagination feature to limit the results to 5 per page, with the associated links available. The issue I am running into, is when I click on page 2, to see the next 5 results, it clears the search and shows an empty table.
The route code:
Route::get('/', [VendorController::class, 'getData']);
the controller code:
@MichalOravec oh wow! This worked but it just shows the same five results over the next couple pages.
So I guess i'll work on that now :)
Thank you!!
it just shows the same five results over the next couple pages
You might need to share some of your relevant code to get help on that... it could be an issue of how you order the query.
@tykus sorry about that! I am having user error on my part trying to post the code, where it doesn't just disappear....
$conn = mysqli_connect('localhost', 'root', '', 'vendor');
if (isset($_GET['search'])) {
$filtervalues = $_GET['search'];
$query = "SELECT * FROM vendors WHERE CONCAT(code,name,vendor,description) LIKE '%$filtervalues%' ";
$query_run = mysqli_query($conn, $query);
if (mysqli_num_rows($query_run) > 0) {
foreach ($query_run as $items) {
?>
<tr class="table-data">
<td></td>
<td><?= $items['code']; ?></td>
<td><?= $items['name']; ?></td>
<td><?= $items['vendor']; ?></td>
<td><?= $items['description']; ?></td>
<td><?= $items['expenditures']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="6">No Record Found</td>
</tr>
<?php
}
}
?>
@AltheaGiam What does this have to do with Laravel?
@MichalOravec oh, if I incorrectly labeled this, I will remedy that. I marked it as so because I am using Laravel framework to do all my work in, since I recall it had the pagination feature that I knew I would be using going forward...
@AltheaGiam you're not making very much sense... what are we doing here?
@tykus I'm sorry about all this! I got the answer I was originally looking for from @michaloravec .
I'll delete this thread shortly, since it got a little off-topic (sorry again) I do appreciate the very quick and prompt replies :)
@AltheaGiam If you got the answer, then mark this thread as solved by choosing the best reply. And don't remove it.
@AltheaGiam With your code you need to write your own paginator. But it's easy to write one in fact one of the easiest things there is to do. Just basic math.
And appending is for laravel paginating, in your custom paginator you will have to append manually. Totally two different things.
And could you please explain how your custom query is laravel related, just curious. Laravel uses pdo.
@AltheaGiam why are you use a in-the-html-query-to-get-data thinggy?
It is something died out for decades.
@jlrdw oh, okay! and I'll be honest with you, I'm still very new at all of this, so I'm sure there are a lot of things I could have done differently but I was looking up a bunch of different answers online and this way seemed to be doing what I needed to do (at the time anyways)
@Lumethys I won't lie, I was looking up tutorials and I guess I didn't pay attention to when they were posted.
@AltheaGiam bad practice, very bad practice, this kind of structuring is an absolute mess. This is not even follow MVC framework pattern, let alone laravel
In Laravel (and any MVC framework out there, even in other language) You define your data structure in Model, process request in Controller and present data in the View.
What you did was none of these, there is no "separate of concern" in your code, and it will not get you anywhere.
I recommend the "Laravel 8 from scratch" Series right on this Laracast website, by taught by one of the most prolific person of Laravel: Jeffrey Way, the creator of this website you are on: https://laracasts.com/series/laravel-8-from-scratch
hi dear friend
use like this
in controller $post = Post::paginate(8); return view .....
in blade
{!! $post->links() !!}Please or to participate in this conversation.