how make a laravel pagination component? - laravel

I have a slider component with a back and forward button and I want to get data in sort of pagination how can I do this?
I just want to do this without using Vue.js or livewire just laravel , JS and JQuery.

Laravel's Query Builder provides a paginate method that gives you a LengthAwarePaginator. Normally, you would render this in a blade view with $items->links(), but instead you can convert its result to JSON (https://laravel.com/docs/8.x/pagination#converting-results-to-json). That way you can use the results in JavaScript any way you'd like.

First, why using slider to create a pagination? there are better approach like lazy load, filter column, and simple pagination. lazy load is best for performance i guess.
If you insist to create a slider component, just wondering, how many data that you have? and how many data that you want to retrieve to screen? Imagine, you have more than 1.000.000 rows in your database, you sure wouldn't want to query for them all for real time updates to your screen. Why i tell you 'query them all'?
Let me tell you, if you do a pagination using laravel default pagination (using limit and offset), the flow behind this is like this:
DB will select all of your rows that you have
DB will limit your row depends on limit parameter
DB will scan row one-by-one to match the offset that you want
DB will retrieve the data that you want
With that flow, that's why pagination (limit & offset) is not good enough if you have a lot of data. The solution is change your pagination logic from limit & offset to query like this (case: auto incremental id):
SELECT
*
FROM
payments
WHERE
Id > 15
LIMIT 20
or for descending approach, you can query like this:
SELECT
*
FROM
payments
WHERE
Id < 50
ORDER BY Id DESC
LIMIT 20
Then in laravel, send a json that include a pagination data. So, in javascript you just have to align the pagination data with library or your own pagination logic.

Related

Where can I find information about Laravel pagination vs datatables pagination?

I have been working with some functional datatable examples, they work perfectly, they are able to handle any number of records and seems that they don't take care about pagination, or that's what I can see.
When I translate those examples, as they are, to my own code, laravel pagination seems to take control, so by default I cannot work with more than 15 records with datatable (great looking pagination controls), so I have to use php ->links() for being able to keep seeking for records, but now I have 2 set of pagination controls and they are not synchronized. The datatable controls are now a subset of laravel's (I can use protected $perPage at my model, but it only changes the number of records with laravel and datatable still has a separated pagination set of controls ). Are there some readings or articles about, I want that my datatable manages pagination as in the examples I'm working with. I've being looking for information but maybe is too obvious.
Thanks in advance.

Is there a way to convert pagintation to collection/array/json Laravel

Is there a way to convert a pagination to a collection/array to get all items like:
Users::where(...)->paginate(15); //paginated
Output: 15 per page
Users::where(..)->get(); //not paginated
Output: all Users per page
So i is it possible to do something like that:
Users::where(...)->paginate(15)->removePagination();
Wanted output: all Users per page
You have 2 choices:
either retrieve the complete query result by chaining ->get()onto your query.
call ->paginate() to retrieve a pagination instance, which automatically retrieves the first page, and it automatically links the example.com?page=1 url param.
(there are also some other functions that complete the query like ->pluck() or ->value() which might not be applicable to you)
As far as I'm aware you cannot revert to the complete collection instance when you have the pagination retrieved (\Illuminate\Contracts\Pagination\LengthAwarePaginator), nor does it make sense to do so since the idea is to retrieve as little data as possible such that your application stays fast.
Maybe you should explain what you're trying to do; for instance you can retrieve the total count in the paginator using SomeModel::query()->paginate()->total(), among other things. You might not need the complete query after all.

how to get total results in laravel pagination?

I have collection $data=$qwery->paginate();
Further in the loop, I send an api request for each element and receive data, which I use with setAttribute to add to the current element of the collection.
In my blade file, I display the information in the form of a table. The last row of the table should contain the total amount of the field that we added using the api request for all elements on all pages. Can I get all collections of all pages in the controller and then calculate the amount using the standard collection method? I tried to use get() instead of pagination, but the request is taking too long, due to API. I can calculate the amount I need for the current page. But how can I find out the amount for all pages at once? I do not ask for a ready-made solution, just your ideas
Use $data->total() to get the total number of items in your paginated results.
Read more here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods

I have table in mysql with 20000 records. How can i retrive it faster

I have table in mysql with 20000 records.
How can I retrieve data faster?
I used datatable plugin for pagination and I also use codeigniter.
It took about one minute to split records to about 4000 page
please use pagination [ci Pagination, php pagination, jquery pagination ajax(faster than others) ] if you are using bootstrap datatable its not working because datatable fetch all the data in one hit after that it breaks data on view which is time consuming you need to break the data in small groups like 20-20 or 50-50 and you need to fetch data in every hit of pagination numbers.
if you are usign ci pagination it will reload on every page hit however if you are using jquery pagination it help you to fetch data in every number hit without refreshing the pages.
please go through the following link: https://phppot.com/php/ajax-pagination-with-php/
in order to implement ajax pagination.
Create view and load table headers
add id on tbody
start ajax pass number want to break = 50, and page = 1
send those value to controller and fetch data as per page.
in your modal you need to add limit and offset which will revised on every hit of pagination number.

dashboard timeline

I'm trying to implement a dashboard similar to facebook in cakephp (getting posts and post them to timeline and while you press see more it keeps retrieving posts from previous offsets) , but im still confused about the logic and tools , should i use the cakephp pagination class in my implementations.
$this->paginate();
it somehow should be called through ajax accourding to some performance wise
Any helps or suggestions where to start from ?
Thanks All
Don't use paginate
If you paginate something that you are prepending data to - you're going to get data overlapping such that you ask for page 2 - and get the end of, as far as the current user is concerned, the previous page.
Use a timestamp
The normal technique for an endless stream of data is to use a query like:
SELECT *
FROM foos
WHERE created >= $previousLastTimestamp
ORDER BY created DESC
LIMIT 20
Note that while I'm using created in this example - it can be any field that is pseudo unique.
When you first render the page, store the timestamp of the last entry in a javascript variable, then your "get more posts" logic should be:
Make an ajax (get) request, passing the last timestamp
Perform the above sql query (as a $this->Foo->find call)
in your js update the last timestamp so that you know where you are up to for the next time the user clicks "get more posts"
The reason to use a >= condition is that, unless the field you are testing against has unique values, it's possible for there to be multiple rows with the value you're testing for. If you have a naturally-unique field that you are sorting by (id) then you don't need to use greater-or-equal, you can simply use greater-than, and avoid needing to think about duplicate rows.
Here's a reference which explains in more detail why you should handle systems like this avoiding traditional pagination.

Resources