Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,440 --> 00:00:01,390
In this lesson,
2
00:00:01,390 --> 00:00:05,310
we will create a Post model so that we can store blog posts to the
3
00:00:05,310 --> 00:00:10,040
database. If you remember from the previous lesson, Model class is just
4
00:00:10,040 --> 00:00:12,950
an abstract presentation of some database table.
5
00:00:13,340 --> 00:00:16,910
However, our blog database is empty.
6
00:00:16,920 --> 00:00:21,790
We need to create the posts table before we can use it. To create a new
7
00:00:21,790 --> 00:00:25,490
table with Laravel, we need to utilize migrations.
8
00:00:25,720 --> 00:00:28,250
You can make a new migration with Artisan.
9
00:00:28,640 --> 00:00:32,369
The command follows the same principle; we type make,
10
00:00:32,380 --> 00:00:37,500
followed by what we want to make, in this case migration, and then
11
00:00:37,500 --> 00:00:42,050
we specify the name for that new migration. And we will use this
12
00:00:42,050 --> 00:00:45,410
command later in the course; however,
13
00:00:45,420 --> 00:00:51,120
in this case we can use one command to create a model and migration at
14
00:00:51,120 --> 00:00:55,970
the same time. I can use this artisan command to create a new Post
15
00:00:55,970 --> 00:01:00,110
model, but if we also add this ‑‑migration flag,
16
00:01:00,120 --> 00:01:04,319
Artisan will assume that we want to make a new migration too, and this
17
00:01:04,319 --> 00:01:09,300
migration will create a table in the database for the Post model. The name
18
00:01:09,300 --> 00:01:12,260
of the table will be derived from the model's name.
19
00:01:12,540 --> 00:01:17,760
Migration will put the name in snake case and make it plural.
20
00:01:18,440 --> 00:01:22,430
Model names are usually in camel case, so if our
21
00:01:22,430 --> 00:01:25,360
model had two words, like BlogPost,
22
00:01:25,370 --> 00:01:30,970
both words would be capitalized like this, and the corresponding
23
00:01:30,970 --> 00:01:35,910
table name should then be plural and in snake case, like this.
24
00:01:35,910 --> 00:01:40,170
Snake case makes all of the letters lowercase and it uses
25
00:01:40,180 --> 00:01:42,260
underscores to separate words.
26
00:01:44,340 --> 00:01:46,760
Okay, so after you run the command,
27
00:01:46,770 --> 00:01:51,160
you can see that the model is successfully created and the new migration
28
00:01:51,170 --> 00:01:54,610
is added to the collection of existing migrations.
29
00:01:54,740 --> 00:01:58,160
Each migration has a timestamp in the name so that
30
00:01:58,160 --> 00:02:00,560
Laravel knows how to order them.
31
00:02:00,910 --> 00:02:03,790
The purpose of the migration is also clear.
32
00:02:03,800 --> 00:02:06,660
It says create_posts_table.
33
00:02:07,940 --> 00:02:11,330
Let's check out this migration file in our project.
34
00:02:11,360 --> 00:02:16,550
You can find all of the migrations inside of the database, migrations directory.
35
00:02:17,840 --> 00:02:21,810
Notice that we already have some default migrations for users,
36
00:02:21,810 --> 00:02:24,620
passwords, jobs, and tokens.
37
00:02:24,910 --> 00:02:29,550
These are provided by Laravel, but we don't need to worry about them now.
38
00:02:29,560 --> 00:02:32,660
They are just here because Laravel assumes that most
39
00:02:32,660 --> 00:02:35,060
applications will need these tables.
40
00:02:35,340 --> 00:02:38,360
Let's click on the new migration to see what it looks like.
41
00:02:38,740 --> 00:02:42,850
Just like we discussed in the previous lesson, here is the up method that
42
00:02:42,860 --> 00:02:47,600
actually defines what the migration will do. With the help of the Schema
43
00:02:47,600 --> 00:02:50,750
facade, we are creating the new posts table.
44
00:02:51,440 --> 00:02:54,600
These two columns are provided by default.
45
00:02:54,850 --> 00:02:58,390
The id column should be present in every table.
46
00:02:58,400 --> 00:02:59,860
It is the primary key.
47
00:03:00,240 --> 00:03:03,250
The timestamps method creates two columns,
48
00:03:03,250 --> 00:03:07,780
one for recording the time when the row is created and one to
49
00:03:07,780 --> 00:03:10,760
record when that specific row is modified.
50
00:03:11,940 --> 00:03:16,230
We can leave these two methods and also add two of our own to
51
00:03:16,230 --> 00:03:18,960
create the title and the description column.
52
00:03:19,340 --> 00:03:23,320
The title will be a simple string, but the description could be a
53
00:03:23,320 --> 00:03:26,260
longer text, so I will use the text method.
54
00:03:26,640 --> 00:03:31,270
This Blueprint table instance has a special method for each type of data and
55
00:03:31,270 --> 00:03:34,860
you can find all of the available column types in the Laravel's
56
00:03:34,860 --> 00:03:39,510
documentation. Search for the available column types in the section about
57
00:03:39,510 --> 00:03:44,300
migrations, and there you should find this list. As you can see, a lot of
58
00:03:44,300 --> 00:03:48,490
different column types can be defined with these methods, so you can be as
59
00:03:48,490 --> 00:03:50,060
specific as you want to be.
60
00:03:50,310 --> 00:03:54,330
Our table is simple, so I will just use these two methods
61
00:03:54,330 --> 00:03:56,660
and leave everything else as it is.
62
00:03:57,540 --> 00:04:02,060
The down method will simply drop the posts table if it exists.
63
00:04:03,140 --> 00:04:05,660
Okay, now that we defined the migration,
64
00:04:05,670 --> 00:04:10,890
we also need to run it to commit the changes to the blog database. To run
65
00:04:10,900 --> 00:04:14,790
all of the migrations from the migrations directory, you can use the
66
00:04:14,800 --> 00:04:19,550
artisan migrate command. Along with the posts table, this will also create
67
00:04:19,560 --> 00:04:24,580
all of the other built‑in tables in Laravel, like the users table that we
68
00:04:24,580 --> 00:04:26,160
will use later in the course.
69
00:04:26,540 --> 00:04:27,780
If you get an error,
70
00:04:27,790 --> 00:04:32,170
that probably means that you didn't configure the database correctly. Try
71
00:04:32,170 --> 00:04:35,540
to find the reason why this error occurred and fix it.
72
00:04:35,660 --> 00:04:36,640
If you get stuck,
73
00:04:36,640 --> 00:04:40,130
you can write me a question in the discussion. But if all of the
74
00:04:40,130 --> 00:04:44,810
migrations run successfully, then congratulations, your database
75
00:04:44,810 --> 00:04:48,970
connection is set up correctly. To check what these migrations did,
76
00:04:48,970 --> 00:04:53,720
you can inspect the blog database from the SQL command line, or if
77
00:04:53,720 --> 00:04:55,250
you have a setup like me,
78
00:04:55,250 --> 00:05:00,220
you can use phpMyAdmin and click on the blog database. Here you can see the
79
00:05:00,220 --> 00:05:07,680
new posts table along with the other built‑in tables provided by Laravel. And
80
00:05:07,680 --> 00:05:11,980
it seems that this table has all of the columns we required, so now we can
81
00:05:11,980 --> 00:05:14,350
finally start working with the posts model.
82
00:05:15,540 --> 00:05:20,610
You can find it inside of the app, Models directory. Here we already
83
00:05:20,610 --> 00:05:24,620
have the built‑in User model and our own Post model.
84
00:05:24,870 --> 00:05:26,420
If I click on the Post file,
85
00:05:26,430 --> 00:05:30,050
you can see that the Post class doesn't really have a lot of code.
86
00:05:30,360 --> 00:05:34,310
Most of its functionality is extended from this eloquent
87
00:05:34,320 --> 00:05:36,920
Model class. In the previous lesson,
88
00:05:36,930 --> 00:05:41,720
I told you that this Model class has a property mapped to each column from
89
00:05:41,720 --> 00:05:45,820
the posts table, but where are these properties defined?
90
00:05:46,100 --> 00:05:48,070
The answer is they are not.
91
00:05:48,080 --> 00:05:52,230
These properties are dynamically generated from the column names when
92
00:05:52,230 --> 00:05:57,290
you use this Model class. Also, Laravel will assume that you want to map
93
00:05:57,290 --> 00:05:59,960
the model to the table with the same name.
94
00:06:00,240 --> 00:06:05,500
Well, not exactly the same, the table name is in snake case and it's plural,
95
00:06:05,500 --> 00:06:07,050
but you get what I mean.
96
00:06:07,840 --> 00:06:11,800
If you want to go outside of this norm and name your table
97
00:06:11,800 --> 00:06:15,160
differently, you can specify the table name inside of the
98
00:06:15,160 --> 00:06:20,230
protected table property, but unless you have a good reason to do that,
99
00:06:20,240 --> 00:06:22,520
it's always better to stick to the standard.
100
00:06:22,530 --> 00:06:24,060
It makes everything easier.8882
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.