Infinite Scroll Loading with Laravel Livewire!
Вставка
- Опубліковано 2 жов 2024
- In this video I build a Laravel application with an infinite scroll table from scratch. I install TailwindCSS, Livewire and the basic setup needed to make this work. I also go over some cool Livewire features that make building this type of application very easy.
Source: gist.github.co...
My Blog: davidgrzyb.com
Cloudways: www.cloudways....
Hey everyone, I built this without much preparation and forgot to pass the options to the IntersectionObserver at 20:34. The reason the demo works is because the options I defined are the default options for the Observer. Sorry about that! 😅
1:15 - Creating new Laravel project
2:52 - Seeding the Database
3:37 - Installing TailwindCSS
6:45 - Livewire Installation
7:32 - Main Template File Creation
10:52 - Creating Livewire Component
11:46 - Creating our route
12:20 - Valet Link & Secure
12:42 - Building Component Class
15:53 - Building template
19:50 - Implementing wire:loading
20:34 - IntersectionObserver implementation
25:34 - Final Demo
Great video, David.
This way is mess, perfectly you need to store items per mage and number of pages loaded, in query only take count of items per page and add pages count, in front you need to render only items that added but do not touch already loaded, I don't know how to do this using livewire but You can do this by returning only html of new items from addMore method in promise and call skipRender() to prevent entire component re-render. After item beign returned, add html by javascript to the end of page
Thanks for the video, really informative. I'm slightly concerned about performance though. What if you have 10 million records and the user keeps scrolling. This implementation will query for 10 million records. Is there a way for Livewire to use a cursor?
Hey, sorry about the late reply. Every scroll makes an additional query and limits the number of returned records (take() is limit()). I don't think querying all the record at once and managing them in memory would be a good solution, it could cause memory issues. That leaves us with the cursor() option, but that will also query, and will query for each record IIRC. I'm really not sure what the best solution here would be 😅
@@DavidGrzyb thanks for the video. lets say first load will get 10 records, second load will get 20 records? is the second load will only get 11 to 20 records only or the second load get the first 10 record again from db?
You said it very well
thank you
this is not cool, it will fail miserably when you add real content and have more than 30 rows of data...
Please make a project with laravel liveware.....
Thank u ❤️❤️❤️
i really like your tutorials, simple and very friendly. Can you say also settings from vscode and plugins and theme? Thanks!
Hi, my latest video goes over this!
Please answer me... where is declared LOOP variable?
The $loop variable is available within the blade for loop, it's not declared anywhere
@@DavidGrzybI thank you, much
... Greetings from El Salvador...
Great video. Subscribed.
But this gets very inefficient the more you scroll down. You're basically loading from 1 to 10, then from 1 to 20, then from 1 to 1000, and not just 10 more at a time.
Thanks for the sub! It's a fair point, and one I didn't think about for the tutorial. Makes me wonder if storing all those records in memory and appending to them would be that much better though, because you still have to make the query 🤔
@@DavidGrzyb you dont need to store the old ones. You just query the new page and only append the new page to existing html.
@@DavidGrzyb @mskhosh u cant do this withing the same component u need a child and the child need a conditioned render return one for when it initialize and one for when u load items and when u load items u shouldnt load the actual child u should load another component with the child included on it again so the data will stay and wont get re-rendered u should also pass vars to the child each time these vars should be the number of data it should return and the number of data it have returned already and use skip() and take() instead of paginated
@@NulledExp is there a tutorial for this?
@@stewarty114 don't really know but if you want let me know what u trying to achieve exactly and I could try to help/guide you.
for example, I'm doing a small project without any special architecture just raw laravel with livewire, and the business logic include comments and notifications so i had to implement something like this but a bit more complex(still easy to achieve tho) so basically i made 3 different livewire classes:
*Notifications
*LoadMoreNotifications
*IncommingNotifications
then 5 views so ended with:
*notifications.blade.php
*load-more-notificationsc.blade.php(container that would show the actual notifications loaded when u click load more + LoadMoreNotifications component class again to render again the load more buttom )
*load-more-notifications.blade.php( only containing the Load more button that trigger a livewire event on click)
*incoming-notifications.blade.php(just an empty livewire view but here i implemented Firebase realtime database for incoming notifications that trigger an Emit event to load the new notifications if the notification ID is > to the last id loaded)
*incoming-commentsc.blade.php(this contain the new loaded notifications + LoadMoreNotifications component class again to keep it listening after receiving an update)
so basically notifications will only render 1 view but, the others will render conditionally 2 different views one as notification container and other for the load more buttom and the incoming listener.
this is very easy to achieve livewire is not the best doing things like this but u can make it work its just a bit confusing at some points, you will need to paginate(DONT use withpagination just the default $model->paginate($this->notifPerPage)) on the Notifications and LoadMoreNotifications classes, and share the init vars like -> lastloadedid, notifPerPage , i hope with this you have a better idea on how to achieve this, if need help with the logic i could make a new git repository and share it with you, I'm planing to share most small features/components that I'm currently developing for this project later on
Just started learning livewire yesterday its very easy if you know laravel
Thanks man for this video 😍
That's an awesome video. Thanks for this tutorial and hope we will get more video like this with liveware.
Where is the source code link
The description has been fixed, link is there 😊
for beatiful video thank you.
Thanks for this video. But doesn’t load more always reload all data depending on current loadAmount?
Hey, It reloads the all data up until the maximum (loadAmount), or all data if we scroll to the bottom of the page where there are no more records. My thinking about this is done without one query regardless, so its probably better to requery all records up to the limit instead of querying the next 10 and appending within memory. Manipulating data within memory is also not great.
Is this the efficient way to do so? 1-10 then 1 to 20 then 1 to 1000...
Ultimately it comes down to either re-querying the whole thing or managing currently displayed records in memory and appending to a collection with each new query. Since we would have to do a new query regardless, I decided to just query all the data again. Not sure what would be more efficient, though I guess it would also greatly depend on the query you are running repeatedly.
If I want to addd loading indicator between them while scrolling how can I do this..?
Within the if condition at the bottom of the page, were he's checking if there are more records, maybe add an 'else' and add wire:loading to the element that displays your spinner.
@@AtomCoder okk thank you I'll try this one
use Breeze package and save setup time.
That's exactly what I did in my latest video 😎
hola