Introducing Laravel Sail0:00
Here's something new. If you have a look at Laravel's installation page, you'll now see references to Docker, as well as a tool called Laravel Sail. Think of this as a build script that constructs a container for your development environment. And it gives you everything, PHP, MySQL, Redis. You're all set to go with a single command without having to worry about setting it up locally on your machine. And as we all know, sometimes, for whatever reason, your machine gets borked and it just doesn't work. And eventually, you have to start from scratch entirely because at some point, things got screwed up. Anyways, Laravel Sail is an attempt to solve that by isolating your development environment using Docker. Now, you might be coming from a tool like Valet, where again, on a Mac, you could install these various services like so, brew install mysql, or php. And of course, you can, if you want, list all of
Installing Docker and App0:47
coming from a tool like Valet, where again, on a Mac, you could install these various services like so, brew install mysql, or php. And of course, you can, if you want, list all of your services that you have running. And if that's good to go, stick with it. Nothing more for you to do. You'd only reference SAIL, again, if you want an isolated environment that doesn't even require php to be installed on your local machine. Okay, let's get set up. Step one is to install Docker. It's about a gigabyte, so give it a minute. All right, let's open that. And excuse me, it's a half a gig. And this should be your only core dependency. All right, great. So let's boot that up. Next, let's create a Laravel app. Now again, traditionally, you might do it like this. You'd install php, you'd install Composer, and you'd install the Laravel installer helper globally. And then you would create your app. Again, you can still do that. But if you want everything to
You'd install php, you'd install Composer, and you'd install the Laravel installer helper globally. And then you would create your app. Again, you can still do that. But if you want everything to be isolated, or you may not even have those tools on your local machine, instead, we need to run it through Docker. So with that in mind, have a look here. Laravel.build/example. And you'll see this gives us a bash script where the URI here, or the segment, gets populated for the app name that you're creating. So effectively, it's still running laravel new example, but it's doing it through Docker. Which means if we copy this, I could then say curl in silent mode, fetch this script, and why don't we call it my project, and then pipe that to bash. Give it a run. And we're all set. So notice, dive in with cd into your project, and then you're going to run this new command, sail up. Now, the first time you run this, it might take a minute, but you only have to do
Fixing MySQL Port Conflicts2:32
all set. So notice, dive in with cd into your project, and then you're going to run this new command, sail up. Now, the first time you run this, it might take a minute, but you only have to do that once. Okay, so you can see we got 99% of the way there, but then we do see an error. Cannot start service mysql. Ports are not available. And you might run into this. If you do run into this, again, it's very likely because you have Valet installed, and it's already using that port. So if you want, let's go ahead and stop Valet entirely. And you can see that does stop nginx. Next, again, if you use Valet, you might have things already on the default ports. And yeah, you can see, actually, it looks like I need to clean some of this up, but I do have mysql 5.7 running. So you can either uninstall that or stop it for now. And yeah, this is only something you'd have to worry about if you manually installed these locally. Otherwise, use a different port.
Running Commands via Sail3:19
running. So you can either uninstall that or stop it for now. And yeah, this is only something you'd have to worry about if you manually installed these locally. Otherwise, use a different port. So again, we're going to run sail up one more time, and it should work. We're all set. So now I can go to localhost. And here we go. We now have our project running through the container. So have a look here. In a new tab, I'll open this in Sublime. And if we make a tweak, hello world, switch back, of course, this is all going to work. But now think about it, what about when you need to run commands like php artisan migrate? Well, in this scenario, you may not have php installed on your machine, you want to run that through your container instead. So again, for that purpose, Laravel's sail command is what you should reach for. If before you did php artisan migrate, now you would do sail artisan migrate.
instead. So again, for that purpose, Laravel's sail command is what you should reach for. If before you did php artisan migrate, now you would do sail artisan migrate. So now here's a cool thing. Notice it didn't tell me that no database exists. And that's because as part of the sail up command, it will automatically verify that you have a database ready to go based on your environment configuration. So basically, it makes sure that you have a my_project database. Okay, so next a couple things, you'll find a new Dockerfile build script and your project routes, you can tweak this as you need to. For example, if you want to switch this to PHP 7.4, review the documentation and tweak it how you need to otherwise leave it alone. Next, again, you'll find that vendor/bin/sail executable here. Okay, but anyways, we have successfully migrated our database. So let's now access it with something like TablePlus,
Next, again, you'll find that vendor/bin/sail executable here. Okay, but anyways, we have successfully migrated our database. So let's now access it with something like TablePlus, let's create a new connection. MySQL, the default port should still be 3306. And again, if we switch back here, we should find that where's it 3306. Or again, for any of these arguments, you can override them when you run your sail up command. So for example, APP_PORT here, you could do APP_PORT= something else, and then run your sail up command to configure that. Anyways, TablePlus, we'll call it localhost. Let's test it. Looks good. Check out our databases. And there you go. There's that my_project database, that sail automatically created for us. We're all set. Okay, so now I could do vendor/bin/sail artisan tinker and then say app/models/UserFactory, just give me a dummy user.
Aliasing Sail and Shell5:53
that sale automatically created for us. We're all set. Okay, so now I could do vendor/bin/sail, artisan tinker. And then say app/models/UserFactory, just give me a dummy User. And if I switch back with any luck, we should see it. Now one thing, it's getting a little annoying it's getting a little annoying running vendor/bin/sail every time. So instead, you could either add vendor/bin to your path, or create an alias like this. Either open your .bashrc file. Or if you use zsh, it might be .zshrc, you might look for something like alias. Here we go. Here are some example aliases. Or what I often do is I will source an aliases file. And that's where I store everything. And that file just contains things just like this. But yeah, here you could say alias sail='vendor/bin/sail'. All right, exit out, and then either open a new tab to refresh, or make sure that you source it. Okay, so now
But yeah, here you could say alias sale equals vendor/bin/sale. All right, exit out, and then either open a new tab to refresh, or make sure that you source it. Okay, so now I can simply run sale. And we can see what we have up and running Redis, MySQL, MailHog, that'll show you in a second. And we're good to go here. So anyways, sometimes you may just want to hunt around the container itself. So rather than using sale, to run a command like this, we could instead say sale shell, we're inside there. So what you might want to do might be a little quicker is to shell in here and then run all of your commands directly. And by the way, notice within my container, I have PHP 8, but actually locally, on this machine, I still have 7.4. So again, just remember, if you go into your container, you just go back to your usual commands like you always do. But outside of it, and I'll hit Ctrl D to exit, don't do php artisan
7.4. So again, just remember, if you go into your container, you just go back to your usual commands like you always do. But outside of it, and I'll hit Ctrl D to exit, don't do php artisan because that may not be available on the machine. Instead, use sale to run your artisan commands to migrate to make a model. And you'll see it's just a little bit slower, because again, it's going through the container to execute that command. Okay, just maybe two more things. Sometimes when you're compiling your assets, you might run npm run dev. But the same is true here. This assumes that you have node installed, this assumes that you have npm installed on your machine. Instead, if we're going all through sale, then you would do sale npm run dev. Now that's going to run the command through our container. Oh, and actually, in this case, it looks like I haven't run sale npm install. And there you go. Otherwise, once again, just shell in there. And then interact with it as
MailHog Email Preview8:26
command through our container. Oh, and actually, in this case, it looks like I haven't run php artisan npm install. And there you go. Otherwise, once again, just shell in there. And then interact with it as you normally would. It's the same thing again, make sure you install the dependencies. And of course, there's other things refer to the documentation, you can share your site with others. Or if you need to test stripe webhooks that can be useful using the php artisan share command. But the last thing I want to show you right now is how you can preview emails. I think this is pretty awesome. So let's do php artisan mail:raw "Hello world". And we're just going to send a quick raw email to let's do this message to foo@bar.com from admin@example.com. And then let's say subject: "Check this out". Alright, this is an easy way to quickly send a raw email without having to create a Mailable. Let's have a look localhost. And that sent an
And then let's say subject. Check this out. Alright, this is an easy way to quickly send a raw email without having to create a Mailable. Let's have a look localhost. And that sent an email, which means I can now go to port 8025. And you'll see that Laravel integrates with MailHog here to instantly intercept the email. And this is pretty badass. And actually, I think we should have gotten an OS notification. Maybe I have that disabled on this machine. But yeah, on yours, you will see it. So anyways, instead, if we want to use an actual Mailable, let's say mail to foo@bar.com. send and how about maybe a Goodbye email, we'll need to create that. So php artisan make:mail Goodbye. And let's do a markdown. And we'll call that for you Goodbye. Alright, so let's import that, like so. And then access it. Here it loads a view, we'll just go to that view here. See you next time.
