YouTubevideo· Sep 19, 2024· 3:42· 5,000 views on YouTube

Infinite Scroll in Laravel Livewire

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

9 moments
  1. all right here's a quick one for you how do you do a NeverEnding scroll within larl and Livewire because pagination is incredibly simple that larl gives you out of the box especially within Livewire we have this kind of a different pagination you can jump around individual pages but how do you make it so that you can just kind of have everything on the page at once and then

  2. scroll consistently well why don't we make it happen so here I have a volt Live Wire component with two things first it uses pagination we need that first you can see here that we have the images links but then we have a state a state that's set to per page and currently we have nine per page and then we have this function that says load more and what's Happening Here is we're just saying okay I want to add more on

  3. the per page State now bear with me here this is where we could start to do and like never ending scrolling because we can consistently load more with one incredible plugin for Alpine and that's going to be the X intersect plugin for

  4. Alpine this is a first-party plugin that you do have to uh install and use and if you haven't done something like that within the app.js for a new liveware application you do have to do this uh kind of song and dance where you have to import Live Wire and then start live wire and then in your welcome file you do have to put the Live Wire script config and Live Wire Styles tags as well

  5. and all that can be found on the Livewire documentation to install new Alpine plugins so this is just a way for us to detract everything that Livewire is doing before it starts so that we can install the Alpine plugin because Livewire and Alpine are so intermingled so once you have that Alpine intersect plugin here's what that looks like now in our width function this is just the render function in a typical Live Wire

  6. class with our width function we're ret turning the images and we're going to paginate them based off of the this per page this state and then returning the images and now here is where the magic happens we can say x intersect full and this is just saying when the page when this particular element becomes into view of the page let's run that live wire method load more and while it's

  7. loading the target of load more let's just say loading more posts and that's as simple as it needs to be so if we were to go back and refresh here we're scrolling down and we're scrolling down and we're scrolling down and and we just keep

  8. scrolling it's as simple as that and it all happens because of this intersect plug-in where it's a wrapper for the intersection absor OB server within the browser API to say when this comes into the viewport make something else happen and because Live Wire and Alpine are so interconnected and we have things like this dollar sign wire method to automatically call methods in our Livewire State our methods our PHP

  9. functions up here then we can just make continuous scrolling happen seamlessly Live Wire and especially Alpine within laravel makes this incredibly easy because we have the page inate we have the methods we have the intersect plugin we have everything we need to do to tell the browser what we need to do when we need to do it and it's as simple as that so keep creating

just you here