YouTubevideo· Feb 11, 2025· 12:53

Can You Do This in Livewire?

Watch on YouTube ↗

transcript

30 moments
  1. so one of my favorite questions that I get asked is how would I do this within Livewire and what I mean by that is usually people say okay there's a specific interaction how would you do this within Livewire and while I'm not a Livewire expert I have built most of the apps that I've built within laravel in Live Wire so usually I've come up against a few things that make it a little bit easier or at least ways to

  2. think about how to build things within life wire because it really comes down to not how would you do this within Live Wire it comes down to when would you use Alpine JS and when would you use live wire and that's really what this question that came from David Kramer from Sentry today where I was like actually how would I do this within Live Wire of course I had thought it out in my head but as we all know that's not

  3. necessarily the best indicator of if that's the best way to do something if it's just in your head so I figured let's actually dive into some code let's actually write out how I would do this and it took me a little longer than I expected I had some things I had to do tonight but I figured it out so why don't we uh jump in so David asks I can't Co Jem my way

  4. to this one lville folks if you need SSR for your core app how would you go about implementing the search header here at p.com one of David's apps um I don't know if there's an easy way to say like use inertia for this one component that would be really nice for those who um haven't kept up with me there is mingle JS which is uh basically oh mingle JS which is basically a way to use reactor view

  5. components within a Live Wire application that might be one way of solving something like this there is the ability to use inertia SSR which is what I uh kind of suggested as well so you can have the benefits of inertia with reactor view or spelt but still have it being server side rendered but how would you do this within just plain Live Wire

  6. and of course you do have to reach for Alpine JS but this is what I came up with and before I actually dive into the implementation I first knew that I wanted to do something within teleport teleport or persist usually are the two single page application features I guess within Live Wire so there's teleport which basically allows you to say I want to do something and then push it to a specific place in

  7. my dom um it's powered by Alpine's teleport aspect but that way I could say this search with impeded docomo little loading that you might have seen there sidebar items loading and then within here of course there's this is a full component within here but then there's this component here at the top too that I can search for something and then see all these things here so um

  8. you know if I'm searching now I can see this search here and it kind of just pops in where like then if I back out I still see the previous component and so I didn't build out the full website but the big thing I think that David was kind of getting at here is how would you do these search header for uh this specifically how would you navigate to

  9. specific items in a site bar search and have that be its own separate component so you're still technically in the sidebar if you were to go back you go to that component that was loaded in that main view what would this look like and so I gave it a shot and this is the search demo we have bottles we have locations again this is just kind of

  10. showing that this is all one part of the application from here up to here it's all just one part of the maybe the app um layout and then in here is a slot to slot in the different aspects of the sidebar nav and I'm just showing the a loading screen so this is just sleeping for one second before I actually show any information but that's a full component within like distillers or

  11. Brands I could technically have another search item or just whatever information goes in this component here but then I can start searching as soon as I click into that search automatically we are uh kind of I don't know rewriting everything that was in that brand section of the sidebar I can search uh and then I can maybe you know click on something or I could just go back and we go to That All Brands without changing

  12. anything this is technically all client side uh but the search is still server side we're getting information from the server so I click into this and now I'm in that specific bottle so how would we do this let's see it all really comes down to at least my specific implementation uh this is the way that I think is might be the best there's probably other ways of doing it probably with multiple Live

  13. Wire components but technically the app blade which is this is a layout um I'm I'm I'm using Live Wire volt and folio which is Live Wire volt is single page volt components so single Page Live Wire components so not having a separate app a separate server and view structure and then folio is technically just a page-based routing or file based routing

  14. I should say instead of instead of having um a specific uh router web.php it's just all in these Pages directory so that doesn't really change too much of the actual implementation it's just how it's going to look when I'm showing it so I this app. blade. PHP which is a layout file um it's just while it's technically a volt component it's mostly

  15. just because of how I had it set before there is no uh at volt which is kind of giving it interactivity but here's the kicker to say so this is the whole side bar as well as search so we're implementing a Livewire search component there which you might see comes over to uh to here right here Live Wire search but it's this m M that has a slot

  16. well what's happening here uh let's see so we have this main of slot with alpine data X data is searching false and if the search- open. window uh we're going to change his searching to true or we're going to change his searching to false and this at search- open. window is an uh an Alpine event that we're listening for specifically from the Live Wire

  17. search component so what that's doing is basically if it searching is true again in Alpine completely client side because this is a blade a server render component within client side we're saying we're going to show the slot basically whatever is in bottles Brands distillers locations Etc or if it's not then we're going to pass in an ID of search content and this is really crucial to the actual

  18. implementation so in our Live Wire search component I'll pop this open or pop uh close this up a little bit Live Wire search component there's a couple of things first we're getting the URL State here and then this is a classic Live Wire search where we're saying um we're just returning all of the bottles that we had listed U with that are like the search parameters but then I have this um updating property as well as

  19. this reset search property and basically this is where we kind of intermingle goal server side Live Wire and client side Alpine um again it might not be as clean as using something like react where that whole layout is going to be uh is going to be a client side component that we could technically server render but I think it is fairly simple and maybe fairly clean basically

  20. what we're doing here is um let's let's start with this x data so we're we have this search- open. window and that's a event that we're having that we're watching if it's open is true open equals false when we when we close that window and then we are saying we're uh inputting this data so if we have the

  21. input Focus we're going to dispatch the search- opened event um and then if we close that's the button to the side of the search then we're going to reset the search and in that reset search we're also dispatching search closed again that's going to that uh app. blade. PHP where we're seeing this uh open. window

  22. closed. window so search closed and then search opened and right here that's what we are doing as well so search content teleport this is the big kicker here because we're teleporting it to that specific div um right right here x show is searching we're going to import that

  23. search content div and basically here is where we get to actually show all the bottles so relatively simple for the most part and I can uh link this specific code uh in the in a GitHub repo as well but the the big kicker here is it's less about how the implementation is working and which parts are Alpine

  24. and which parts are actually Livewire Live Wire is technically only handling the actual structure and navigation of the whole application it's still all server rendered and while you can't turn off JavaScript and still get the same experience you would still be able to um you still live wire is handling all of the navigation with wire navigate to get

  25. these um to get these navigations within each page like the bottles blade. PHP page it is a lazy component so that just means that uh we are going to load everything else in this case the app the uh x- layouts. apppp before we load everything in the page itself and basically this is what allows us to have now that we have this Live

  26. Wire of the actual search but the Alpine to trigger the actual uh teleport skeleton I guess if you want to say it to be able to overwrite or replace whatever information is putting in that specific slot so again click in and we can you know search for something specific but click back and we get that original slot so this is all right here

  27. is client side I could technically you know inspect and we could um see here that this uh this div um it's probably because I clicked out so we're not seeing it as well but that that's all client side uh interaction happening if I was to click into it we see that clicking back clicking into it click back and we see that navigation happen again that

  28. all this search is still happening on the server but the actual displaying of the search contents is happening on the client that we're replacing the original server rendered slot so I hope this gives a little bit of insight as to how you might do something like this within Live Wire and Alpine JS um and that way you have a little bit more of a modern

  29. feel within an application is it the best solution for this maybe it's not even the best solution within Live Wire but I know Live Wire pretty well and so if I was to build this within inertia it probably would take me a little bit longer just because I haven't I don't have as much practice within certain elements like this so is it possible yeah is it the best probably not but for a application where you just need this

  30. for one component it doesn't do too bad so yeah hopefully that gives you some insight as to how to use Alpine JS Live Wire in order to teleport something and then also in order to send events to do something within actual Alpine client side data

just you here