- Overview
- Transcript
3.5 Adding a Message Title With Migrations
Migrations allow us to modify our database. We're going to create a migration to add a title column to our table. We'll also update the rest of our application to incorporate the new column.
1.Introduction2 lessons, 09:51
1.1Introduction02:09
1.2What You Need07:42
2.The Static Site Project2 lessons, 18:20
2.1View and Routing Basics10:22
2.2Layouts and Sub-Views07:58
3.The Shoutboard Project5 lessons, 53:09
3.1Database Configuration and Migrations14:23
3.2Creating the Model09:38
3.3Creating Messages14:20
3.4Viewing Messages07:41
3.5Adding a Message Title With Migrations07:07
4.A Ticket System6 lessons, 1:12:09
4.1Introduction to Controllers09:02
4.2Setting Up Our Database16:09
4.3Viewing a List of Tickets08:51
4.4Viewing and Editing Individual Tickets13:08
4.5Adding Authentication10:01
4.6Linking Tickets With Users14:58
5.Conclusion1 lesson, 01:22
5.1Conclusion01:22
3.5 Adding a Message Title With Migrations
In the previous lesson we started displaying our messages. And we did so by not only displaying the message itself, but also the date and time as the title, if you will. And we realized that that really wasn't the best thing. It would be great if we allowed users to specify their own title so that we could have a title and a message. That just makes sense. So we are going to add a migration that is going to add that title to our database. Otherwise, how would we store our title? So we want to create a new migration. We're going to use Artisan. And we want to make a migration. And, as far as the name is concerned, we want to be descriptive as to what we are doing. We are adding title to messages. And just like before, this is going to create a new file inside of our project in the database folder, in the migrations. And well, it's not listed here. Let's refresh. And there it is, we have our new migration. Now in our previous migration, we used Schema::create to create the messages table. That's not what we want to do in this case. Instead we are going to use a method called table, instead of create. So whenever you use the create, you are of course creating something. Whenever you use the table, we are modifying whatever table that we specify. So in this case it's going to be our messages table. And, as far as the code inside of this method, we simply just specify our field. So this is going to be a string. And then we specify the name of this column, it's going to be title. And let's also specify a length of 50, that should be sufficient. Now since we are using MySQL, we can also say after. And this is the feature only for MySQL databases. But if we wanted to order our columns, so that instead of just putting this title column at the end we could specify where we wanted to put it. So if we wanted to put it in between the email and the message, which actually makes sense to do so, than we could use after. And than specify the email column, so that our title column will be put in between email and message. Now, that's the up method. We also want it to fill out the down method, so that, if we ever roll back our migration, then we will do that. So we will once again use the table method. We will specify our messages table. But instead of calling the string method, we are going to call dropColumn. And we will specify the name of that column to drop. Title in this case. So that is going to be our migration. And so now we can go back to the command line, and we can say php artisan migrate. And that is going to actually migrate our database. So if we go look at our database now, let's refresh this. And we are going to see that we now have ID, email, title is right there. Message, created at, and updated at. Now, because we have a new field, that means we also have to update our model class. So let's go to our app folder and let's open up Message.php. Because we have the fillable array set so that we can use mass assignment here. And we will specify title as well. This also means that we need to go to our create view and add a new field there. So we're going to go to resources > views, and then create. And inside of our form we want a title field. So let's just copy what we have for email so that we will have Email, and then Title, and then Message. But of course we need to change some of these values. So to keep with what we have used, as far as the names are concerned, we'll say Input and then title. The text box is going to be of type text. And then the name is inputTitle, inputTitle. Now as far as the label is concerned, that's just needs to be to be Title. And that will be good to go. Let's get rid of the placeholder. And now we can just go to our routs, because that is where we are dealing with information. Although, that's not actually true. We need to go back to our app folder. We need to go to Http and to Requests, because we need to modify our rules here. If we are going to require an email address in a message, we just might as well require the title as well. So we will use the required flag there. And then we will go to our routes. So let's go to routes, and then web.php. And inside of our post route for create we are going to assign a title. So we're going to do exactly the same thing that we have done before, except that we are going to get('inputTitle') and set that to our title. So I believe that's everything. If we start our application, let's say php artisan serve then that will start our application. We can go to the browser. Let's create a message. We see Title now, so let's say that this is going to be from fu at bar.com. This is the title. And The first message with a title. Although, we did not change how we display our list here. We are still just displaying the date and time. So we need to also modify our welcome view. So let's go to the resources > views, and then welcome. In stead of the created @, we are going to use the title. Now for those those that don't have a title, it's not going to have anything there because there's no data for those preexisting messages. But in this particular case, that's okay. But now we have a title field. And we added that to our application and our database very easily. We used migrations in order to add that column to our table. And then we modified our application to take the title into account. Now, of course, in a larger application it's going to be a little bit more involved. But it's the same process. Setup your migration, perform your migration. And then modify your application to take that migration into account.







