Playing
01. Installation, database and migration refresher

Episodes

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

Transcript

00:00
If you are very new to Laravel, this episode is just going to be a refresher on some of the core concepts that we're going to need to be able to follow the rest of this course. So we're going to go ahead and first of all, talk about how to set up a fresh Laravel project.
00:15
Then we're going to go ahead and configure our database. We're going to talk about migrations and we're going to talk about the command line and what gets created when we create a new model, which is the basis for Eloquent. So, of course, if you already know this stuff, feel free to move on.
00:30
But otherwise, let's take a look. OK, so the first thing that we need to do is decide on a way that we're going to install a Laravel project. If you are very new to Laravel, I'd recommend just using the Laravel installer here
00:43
and then running this locally with something like Laravel Valet. Now, there are a ton of different ways that you can get a Laravel project set up and ready to view in your browser, depending on your operating system or the way you're working. But I'd go ahead and find out the one that just works for you.
00:57
And then, of course, you can go from there. So the first thing that we need to do is get a Laravel project installed. I prefer the Laravel installer for this, but you can go ahead and install Composer and just create a fresh project directly from Composer as well.
01:10
Either of these commands will work. But the easiest way is to globally install with Composer the Laravel installer and then we can just use the Laravel new and then the name of our app. So I've gone ahead and already done that.
01:23
So I'm going to go ahead and say Laravel new and then I'm just going to pull out the project name. So let's just call this Laravel project just as an example. So as soon as we hit that, that's going to create a directory with all of the files that we need to get this working.
01:37
And because I am running Laravel Valet, this will automatically be available at this URL for me, Laravel-project.test. So if I go ahead and run this now, you can see, sure enough, we have a fresh Laravel project. Now, if that is not the case, then what I would recommend you do
01:55
is go into the directory where you've just created this, so Laravel-project and you can just use phpArtisan and serve. What that's going to do is it's going to serve this at this URL. And if we click on that, you can see we get exactly the same thing.
02:08
So even if you're not using something like Laravel Valet, you'll be able to very easily follow this course if you just run phpArtisan serve. Then you want another terminal window open somewhere, whether that's in your editor or outside of your editor,
02:21
inside of that project directory, so you can start to run commands. So I'm going to go ahead and cancel this off because I'm using Laravel Valet. And let's talk about our database. That's probably the first thing that we're going to want to cover
02:32
because we are, of course, talking about Eloquent, which hits our database. So let's go ahead and open up the files within this project here. And you'll see this .env file in the root of your project. This contains all the environment variables that your Laravel projects will use.
02:48
And we can come down here to this database section and configure, first of all, what driver we want to use and which database software we want to use. We can change the host, the port, the database name if we need to. Then we can provide a username and password.
03:02
So for me, I always use Postgres. So I can configure this over to use Postgres. I've got my Postgres client just open in here. And again, it doesn't matter which client you're using
03:12
to connect to your database as long as you can. And I just need to switch the port over here as well. So if you are using MySQL, you can leave these two exactly as they are. And I'm just going to lastly switch over the username for my local Postgres server.
03:27
So the next thing you're going to want to do is create out the database. So I've got a ton in here, but I'm going to create a new one called Laravel underscore project. And that's now just an empty database.
03:37
So now we can talk about the default migrations that come with Laravel. So when you install a Laravel project over in your database and migrations folder, this has four database migrations. Now, migrations basically just create your database schema for you.
03:52
So for example, the first one here is create users table. When we run our migrations, this will create a table called users. And we can define in here with our migrations all of the different columns via the methods that we have on this blueprint object.
04:07
So all of these are in the Laravel documentation. Throughout the course, we'll be keeping this really simple, mostly strings, integers, booleans, things like that. But as you can see, these are already defined for you.
04:18
So when you start to use Laravel authentication or you start to use a starter kit, this is available for you. Now, a default installation also comes with a password resets table. Again, for authentication, it comes with a failed jobs table
04:32
when you get to the point where you want to use jobs and queuing. And then it also has a personal access tokens table. So we can kind of ignore these. We're mostly going to be focusing on the users table,
04:42
but you will want to just go ahead and migrate all of these because they'll be useful at some point. So to actually migrate these, we're going to go ahead and run PHP artisan migrate.
04:52
And as long as we have our database credentials hooked up correctly, this is going to go ahead and run through all of these migrations. And as you can see over in here, these have now been created. So as we go through the course,
05:03
we're going to be building up lots of different models to look at lots of different relationship types. So we're going to be going ahead and creating models with migrations. And then we're going to be filling those in and running them.
05:14
Let's take a look at a really quick example of that now, just so we can get to grips with it before we start. So we are going to use artisan, which is the command line tool for Laravel. And we're going to be throughout the course using make model.
05:27
This will create a model, which is an eloquent model, which means that, for example, if we create something like a post, we can use the post model to create posts, delete them, access them, whatever we need to do.
05:39
So we're going to go ahead and give the name of the model that we want to create. And you can do this on its own if you want to. But there's an additional flag in here. There's multiple of these.
05:49
And we can use hyphen m. What that will do is it will create a migration alongside of this and fill in most of the information for you. So it's a really convenient way to create a model and a migration at the same time.
06:01
With models, you're pretty much always going to have a migration alongside of these. So I'd always recommend using the m flag in here to create that out. So when we go ahead and run that, you can see that's created out a model, which we can use to actually access the database.
06:14
And it will create a migration, which allows us to define the schema. So, of course, if we're going to head over to our migrations folder, that's created out a new migration for us. And you can see that the boilerplate code here has already been given to us.
06:26
This will create out a posts table. And it always contains an ID, which is the ID column. That's an auto incrementing primary key. And it also uses this timestamps method,
06:38
which will create out a created at and an updated at date for us. So they are in there ready to go. Now, inside of here, it's really up to you. You can go ahead and create anything out in here.
06:50
For example, we could create a string or a text with the body of the post. And then once we have filled in our migrations and we're happy with the columns that we've got, we can go ahead and run PHP artisan migrate again. It won't run the previous migrations.
07:04
It will just run any migrations that haven't previously been migrated. So you can see here that that is going ahead and just running that. If we head over to the database, sure enough, we have a posts table with the columns that we need.
07:17
It's easy as that. So we'll be going through all of this in pretty much every section of this course. But if you are new to Laravel, hopefully that has helped give you a refresher on the most basic concepts that we need to get started.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.