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:02,650
It's time for the summary of this module.
2
00:00:02,880 --> 00:00:07,550
In this module, we established a connection to the real SQL database.
3
00:00:07,940 --> 00:00:08,610
First,
4
00:00:08,620 --> 00:00:13,070
we discussed the supported database management systems you can use in Laravel.
5
00:00:13,630 --> 00:00:17,000
You need to choose one of them and install it,
6
00:00:17,240 --> 00:00:21,940
but make sure that the installed version is supported and check if PHP has some
7
00:00:21,940 --> 00:00:25,660
special dependencies for working with that database system.
8
00:00:25,940 --> 00:00:27,070
Once you do that,
9
00:00:27,070 --> 00:00:30,860
you can configure the connection inside of the database PHP file.
10
00:00:31,240 --> 00:00:35,970
However, since all of the actual data there is pulled from the env file,
11
00:00:35,980 --> 00:00:39,160
we need to update these environment variables.
12
00:00:39,540 --> 00:00:42,680
Here, we can set the specific system, database host,
13
00:00:42,690 --> 00:00:46,050
port, database name, and access details.
14
00:00:46,440 --> 00:00:50,460
Standard practice is to name the database after the project.
15
00:00:50,840 --> 00:00:54,620
You need to create this database in your system of choice by
16
00:00:54,630 --> 00:00:58,760
either using command line interface or some other graphical
17
00:00:58,760 --> 00:01:01,160
software for managing databases.
18
00:01:01,430 --> 00:01:06,460
We can check if the connection was established by using the artisan db command.
19
00:01:06,840 --> 00:01:09,600
If this command redirects us to the native command
20
00:01:09,600 --> 00:01:14,800
line of the database system we use, that means that the connection is successful.
21
00:01:15,040 --> 00:01:16,790
Once the connection is established,
22
00:01:16,800 --> 00:01:19,860
you can start using the database through Laravel.
23
00:01:20,140 --> 00:01:22,840
DB façade, also known as Query Builder,
24
00:01:22,850 --> 00:01:27,670
allows us to retrieve data from the database by using object‑oriented practices,
25
00:01:27,680 --> 00:01:30,060
instead of writing raw SQL queries.
26
00:01:30,440 --> 00:01:35,520
By chaining methods, we can construct complex queries and get the required data.
27
00:01:35,750 --> 00:01:38,680
We will not utilize the Query Builder in this course,
28
00:01:38,680 --> 00:01:42,000
but you should be familiar with it in case you need to perform some
29
00:01:42,000 --> 00:01:45,450
really complex queries that require high performance.
30
00:01:45,670 --> 00:01:49,060
Also, these query methods can be used on models.
31
00:01:49,740 --> 00:01:54,490
So in this course, we will use models to manage resources from the database.
32
00:01:54,780 --> 00:02:01,050
Model is a simple PHP class that extends the Model class from the Eloquent ORM.
33
00:02:01,440 --> 00:02:05,240
Eloquent object‑relational mapper will map the Model
34
00:02:05,240 --> 00:02:07,650
class to the table in the database.
35
00:02:08,039 --> 00:02:11,570
It will also dynamically create a property for each column
36
00:02:11,570 --> 00:02:15,480
from that specific table so we can use the Model class to
37
00:02:15,480 --> 00:02:17,760
retrieve records from the database.
38
00:02:18,540 --> 00:02:19,310
For example,
39
00:02:19,310 --> 00:02:23,050
the all method will return all of the records from the table
40
00:02:23,060 --> 00:02:25,560
in the form of an Eloquent collection.
41
00:02:25,940 --> 00:02:29,970
This collection object can be looped over so each one of these records
42
00:02:29,970 --> 00:02:32,850
from the database will be represented as an object,
43
00:02:32,860 --> 00:02:34,870
an instance of the Model class,
44
00:02:35,180 --> 00:02:38,350
and then we can use these dynamic properties to
45
00:02:38,360 --> 00:02:40,960
access specific parts of that record.
46
00:02:41,340 --> 00:02:44,670
We can also use this find method to retrieve a single
47
00:02:44,670 --> 00:02:47,700
record from the database by the provided ID,
48
00:02:47,940 --> 00:02:50,820
but of course, before we can even use this model,
49
00:02:50,830 --> 00:02:54,480
we first need to create the corresponding table in the database.
50
00:02:54,480 --> 00:02:59,960
To create new tables and alter the existing ones, we can use migrations.
51
00:03:00,340 --> 00:03:04,450
Migrations are used like a database version control system.
52
00:03:04,840 --> 00:03:09,010
You can create a new migration with the make migration artisan command.
53
00:03:09,120 --> 00:03:13,450
This will create a new migration file with the two methods, up and down.
54
00:03:13,840 --> 00:03:17,050
Inside of up, we define what this migration should do.
55
00:03:17,440 --> 00:03:21,770
We can use the schema and blueprint classes to create tables and
56
00:03:21,770 --> 00:03:24,850
define what kind of columns this table will have.
57
00:03:25,440 --> 00:03:27,950
The down method should do the opposite,
58
00:03:27,950 --> 00:03:32,760
but this method will only be used if you try to roll back this migration.
59
00:03:33,140 --> 00:03:36,870
To run all of the migrations and commit the changes to the database,
60
00:03:36,870 --> 00:03:39,760
we need to run the artisan migrate command.
61
00:03:40,140 --> 00:03:45,190
Once the table is created, we can make a new model to manage the post resource.
62
00:03:45,250 --> 00:03:48,980
You can also just add the migration option to this model
63
00:03:48,980 --> 00:03:52,130
command to create the migration at the same time.
64
00:03:52,260 --> 00:03:55,860
This is only if you didn't create the post table already.
65
00:03:56,140 --> 00:03:59,520
The special migration option will assume that you want to create a
66
00:03:59,520 --> 00:04:02,050
corresponding table with the name of the model,
67
00:04:02,440 --> 00:04:05,810
but of course the name of the model will be converted from the
68
00:04:05,810 --> 00:04:09,160
camelCase to snake case and it will also be plural.
69
00:04:09,540 --> 00:04:12,920
This is also the same standard that Eloquent follows
70
00:04:12,920 --> 00:04:15,450
for connecting models and tables.
71
00:04:15,840 --> 00:04:20,550
By default, the Post model will take data from the post table.
72
00:04:20,640 --> 00:04:25,180
However, if you want, you can change that with the special table property,
73
00:04:25,190 --> 00:04:28,460
but I wouldn't recommend it unless you have some special reason.
74
00:04:29,140 --> 00:04:31,620
After we created the new Post model,
75
00:04:31,620 --> 00:04:35,140
we used it to store a new post to the database.
76
00:04:35,440 --> 00:04:39,000
We took the user input from the request object and placed it in the
77
00:04:39,000 --> 00:04:42,070
appropriate properties of the new Post instance.
78
00:04:42,370 --> 00:04:46,360
Then we committed the changes to the database with the save method.
79
00:04:46,640 --> 00:04:49,050
Only after you save the model instance,
80
00:04:49,060 --> 00:04:52,660
this post will actually be inserted into the post table.
81
00:04:52,940 --> 00:04:55,750
We also learned how to retrieve all of the records from a
82
00:04:55,750 --> 00:04:58,640
specific table by using the all method.
83
00:04:58,740 --> 00:05:01,200
Once we have this collection of posts,
84
00:05:01,200 --> 00:05:06,050
we can pass it down to the template as a second argument of the view function.
85
00:05:06,440 --> 00:05:07,660
Inside of the template,
86
00:05:07,660 --> 00:05:13,160
we can loop over all of the records with the for else or for each directive.
87
00:05:13,540 --> 00:05:15,880
However, if we use for else,
88
00:05:15,890 --> 00:05:19,750
we can also define what will be rendered if the collection is empty,
89
00:05:20,540 --> 00:05:21,780
and that's it.
90
00:05:21,780 --> 00:05:26,660
We finally started putting resources in some kind of permanent storage.
91
00:05:27,040 --> 00:05:27,970
In the next module,
92
00:05:27,970 --> 00:05:30,740
we will continue implementing all of the resource controller
93
00:05:30,740 --> 00:05:33,060
actions with the help of the Post model.
94
00:05:33,440 --> 00:05:34,550
I will see you there.8270
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.