Transcript
-
Our project manager says, "Hey, we're also going to want our users "to have some form of a basic profile "that lists out movies that they have in their watch list
-
"or something of the sort." So now we need a brand new table that we haven't accounted for within our schema or really anywhere. So let's go ahead and add that in next.
-
For a profile, we're going to want it to have its own model because it's not a pivot table. So we'll do node ace make model and let's do hyphen hyphen help here
-
to see the available options. So if you remember back to when we first looked at this, we could add in hyphen m, hyphen c, or hyphen f to add in a migration, controller, and factory,
-
or any combination therein for the model that we're creating. So if we wanted to, we could do node ace make model profile,
-
hyphen m to add in a migration, c to add in a controller, and f to add in a factory or the profile itself. So we'll hit enter there
-
and you can see it added in a profile, a migration, a controller, and a factory, all for our profile. So with this single command, we've now created everything that we're going to need now
-
and in the future for our profile model. Let's go ahead and dive into our text editor, scroll down to our new create profile migration. First, we're going to want this to be related to our user.
-
So we'll do table, integer, user_id, unsigned, references, users.id, and make that not nullable. Now, for the most part,
-
the purpose of this profile model is to serve for an example so we don't really have a whole lot to add onto it, but for the sake of adding something to it,
-
let's go ahead and add maybe a user description to it. So we can do text description, just like so. It's a hefty column, maybe we don't want it to be specifically on the user model,
-
so we'll have this separate profile model that we can add in at any point to our user via a relationship. This could contain additional information like social media information, users' website,
-
business information, stuff like that, if you wanted to, but we'll leave it at just a description for now. So we have that defined here. Let's next go up to our models, profile,
-
we'll add in our column, declare_user_id of type number, and column, declare_description of type string, and I think we left that as nullable,
-
so we'll do that as nullable there. And then we can go down to our profile factory and add in a description column there of faker.
-
We'll just do lorem.paragraphs, like so, and we also have our user_id. For right now, we'll just hard code that as one and give that a save.
-
The last thing that we need to do is run our migration to create our profile table inside of our database. So node.ace_migration_run,
-
and that will run just our create_profiles table. In addition to that, we now also have three batches. So if we run node.ace_migration_status, we'll see a table printed out
-
that has all of our migrations as completed because we've run them all, and we now have batches one, which contains most of our migrations, two, where we altered and added in a sort order
-
to our cast_movies table, And now three where we've created our user's profiles.
Adding A Profile Model, Migration, Factory, and Controller
Uh oh, a new requirement has come in and now we also need to account for user profiles! In this lesson, we'll learn how we can easily create a new model, migration, factory, and controller for an entity in one fell swoop!
Join the Discussion 0 comments
Be the first to comment!