This episode is for members only

Sign up to access "Build an Appointment Booking System With Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
24. Installing Alpine.js

Episodes

0%
Your progress
  • Total: 4h 49m
  • Played: 0m
  • Remaining: 4h 49m
Join or sign in to track your progress

Transcript

00:00
Okay, so to make everything as easy as possible, we're gonna go ahead and pull Alpine.js
00:03
into our Laravel project, so we can use this within our blade templates and sprinkle in the interactivity that we need. If you've not worked with Alpine before,
00:12
don't worry too much about the framework itself. What we're gonna do is show you in this episode just how this works and how we get it on the page, and then the rest of it will just be pure JavaScript.
00:24
So that's pretty much it. Okay, so let's go and get Alpine installed. We're not gonna go ahead and pull this in with the CDN.
00:31
What we're gonna do is head over to our project and we're gonna do an npm install on Alpine.js. So just while that's finishing, let's head over to our app.js file under resources and js.
00:44
At the moment, this doesn't contain anything except the bootstrap import, but what we can do is go ahead and import Alpine directly from here.
00:52
So we can pull it in from Alpine.js that we've just pulled in from npm, and then we can just go ahead and set it as a global on our window object.
01:01
And if you're using a Laravel starter kit, this will probably already be in there for you. So once we've done that, we can go ahead and call Alpine start,
01:09
and that will boot everything up for us, and we are pretty much ready to go. The only other thing that we need to do is over in our app layout,
01:16
make sure that when we included V here, we use resources.js and app.js, so that gets pulled in as well. So let's test together that Alpine is pulled in and working,
01:28
and this will give you a feel for how this works. So we're gonna focus on this section here because in this entire section of the course, we're gonna be working on our availability calendar.
01:37
Let's turn this div here into an Alpine component and just demonstrate how this works. So one of the things concepts in Alpine, if you've not come across it before, is xdata.
01:47
This is where we hold things. For example, our date picker, we can set this to a null value, and then when we've installed our date picker,
01:55
what we can do is say xinit, and basically this whole div now becomes a component where we can look for that input, attach it to our picker,
02:04
and then what we can do is add methods inside of xdata as well to do things with our picker. So xinit, think of it as just this runs when this component gets booted up.
02:15
So for example, I could say something like picker equals ABC and then I can go ahead and do a console log on picker, and let's see what happens. So let's go over and pull up our console
02:28
and you can see, sure enough, we get ABC dumped out. So picker is now set to the value ABC, and when we do get to install our picker, of course here, instead of setting this to a string,
02:38
we're gonna be initializing it and then doing stuff with it inside of here. So that's pretty much all we're gonna cover. We're gonna have a couple of Alpine components on this page.
02:48
In fact, this entire div is gonna become an Alpine component with all of the stuff that we need to send through to our backend when we're making an appointment.
02:56
But hopefully that makes sense, and in the next episode, we're gonna get our picker actually on the page with an input directly in here so we can actually open it up.
03:06
So let's go over and install that and then we'll see more about how we can work with Alpine just to make this a lot easier rather than defining script tags all over the page.

Episode summary

In this episode, we're kicking things off by getting Alpine.js up and running inside our Laravel project. We'll walk through the process step by step: first, we'll use npm to install Alpine.js (instead of just dropping in a CDN link), and then we'll talk about how to import Alpine into your app.js file.

Once Alpine is brought in, you'll see how to attach it globally to the window object and start it up so it’s ready to use in our Blade templates. If you’re using a Laravel starter kit, you might already have this done for you, but we’ll make sure you know exactly what’s happening either way.

Next, we'll do a quick test to make sure everything is working. We'll turn a simple <div> into our first Alpine component, show you how x-data and x-init work, and demonstrate using a mock value with our soon-to-be date picker. This is a nice sneak peek of how Alpine lets you write little bits of JavaScript right where you need them, keeping things tidy and interactive.

By the end of the episode, you'll have Alpine set up and a good idea of how we'll use it throughout the app—especially as we build our availability calendar. In the next episode, we'll actually get our date picker input in place and see more of Alpine in action!

Episode discussion

No comments, yet. Be the first!