This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
27. Working with models

Episodes

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

Transcript

00:00
So we spent a lot of time setting the database up, but the good news is we don't really need too much more work to get models working. Now, models have the benefit of being able
00:09
to create relationships between different models. So for example, if a user has lots of posts within your application or comments, you can very easily set this up. And all of that is on the Eloquent documentation over on Laravel. Okay, let's take a look at creating out a model and switching up what we're doing here so we don't directly have to access the database. Okay, let's go over to
00:31
our main app. And in here, we can just create out a new directory for all of our models, we're just going to have one for the purpose of this application, but you can create more later if you need to. So we're going to create out a PHP class just called user. So we're going to use this as a representation of the user. The first thing we want to do is extend the base model
00:52
from Eloquent. So make sure we do that first of all. And now we pretty much can just use this and all the underlying functionality behind it to access our users in the database. Let's go over to our home controller and switch up what we're doing here to try this out. So we can actually get rid of our database manager now because we don't need to access that directly. And instead, rather than
01:16
doing this, we can just use user and then get. Now on this, we can perform any other filters for our queries or any kind of query structure. And I'll show you how that looks in a second. But let's just see if this works first. So if I give that a refresh, yeah, sure enough, this is working. Now you might be asking if you've never worked with Eloquent before, how is this working when it
01:38
doesn't know what our table name is or any of that kind of stuff? Well, from the model name, it's implied that the table name is just a pluralized version of this. So this knows that this the table name is called users. If you need to change this for any reason, what you could do is specify the table directly inside of here with a protected table property. So give that refresh. It works
02:01
in exactly the same way. But of course, if we change that up to something that doesn't exist, you can see that the table isn't found. OK, so another thing that we can do is if we are inserting records, we can set things like fillable fields. So by default, anytime you create a user and I'll show you how to do that again in a minute, we wouldn't be able to do this because
02:23
we need to tell Laravel or Eloquent which things are fillable. So we're going to say name is fillable. In our case, we'll be adding more to this a little bit later. Or what you could do is unguard this completely. So you could set guarded to false if you wanted to. And that would mean that any of the fields were fillable. I'll go ahead and comment out so you have a reference
02:43
to it, but you can choose which strategy you want to take. OK, so that's not going to make much difference to here. But what we can now do over in our home controller is, for example, every time we land on this controller, we could create a new user to create a model. We're going to go ahead and say user create. I'm going to pass in any of the properties that we want to fill within columns.
03:06
So let's go ahead and create another one now called Billy. And if we give this a refresh, you can see we've got another slight issue where Eloquent needs an updated at and a created at column inside of your models. Now, we'll cover this in a bit more detail later when we look at authentication. Let's just add in the created at and updated at dates now. So created at is
03:28
going to be a timestamp. And we'll duplicate this down and create a new one called updated at. And if we do that, that's now going to fill that in and these will be filled in for when that was created. There we go. You can see that that's been created out there and we have that data, of course, in the database. Now, if you wanted to add any additional functionality to the queries
03:49
that you are creating, you can do that with the Eloquent interface. So, for example, I could say where name equals and then I could say Alex. What that's going to do is it's going to return a collection with just Alex in there. We have a look. You can see that that changes. And that's pretty much it. You can do anything you want with these access relationships if you need to.
04:13
We can even do things like paginate these instead as well, which we'll be looking at later. But if you need to go ahead and learn Eloquent or swap this out, you can find all of this stuff on the documentation. For now, let's just leave this as giving us all of the records in the database and we now have models set up.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

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

Episode discussion

No comments, yet. Be the first!