Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,540 --> 00:00:04,070
Most of the web applications of today need some kind
2
00:00:04,070 --> 00:00:06,430
of permanent database storage.
3
00:00:06,570 --> 00:00:08,430
If you worked with PHP,
4
00:00:08,430 --> 00:00:11,930
you probably used some database management system like
5
00:00:11,930 --> 00:00:15,560
MySQL to store and retrieve data.
6
00:00:15,840 --> 00:00:17,030
And to do that,
7
00:00:17,030 --> 00:00:22,410
you would need to have some kind of a PHP extension such as MySQLi that
8
00:00:22,420 --> 00:00:25,950
allows you to send queries to the running database.
9
00:00:26,340 --> 00:00:29,300
That means that besides programming in PHP,
10
00:00:29,300 --> 00:00:33,960
you would also have to be familiar with constructing raw SQL queries.
11
00:00:34,340 --> 00:00:37,360
In Laravel, we want to avoid doing that.
12
00:00:37,370 --> 00:00:42,980
We want to stay in the domain of PHP and do everything in PHP.
13
00:00:43,250 --> 00:00:46,490
One way in which level Laravel allows us to do that is
14
00:00:46,490 --> 00:00:49,090
by using the so‑called Query Builder.
15
00:00:49,240 --> 00:00:53,360
Query Builder is implemented with this DB façade.
16
00:00:53,740 --> 00:00:54,930
Once you import it,
17
00:00:54,940 --> 00:01:00,550
you can use a chain of methods to construct a query in an object‑oriented way.
18
00:01:00,940 --> 00:01:06,490
So this Query Builder façade will actually write SQL statements, instead of you.
19
00:01:06,500 --> 00:01:10,260
Therefore, all you need to know is how to use these methods.
20
00:01:11,040 --> 00:01:15,750
For example, here, I'm trying to get all of the users from the database.
21
00:01:16,140 --> 00:01:20,770
I specified the users table inside of this table method and
22
00:01:20,770 --> 00:01:23,960
then use get to retrieve all of the records.
23
00:01:24,340 --> 00:01:29,880
This method returns a Collection instance from the Illuminate package,
24
00:01:29,890 --> 00:01:32,880
which kind of works like a regular array.
25
00:01:33,120 --> 00:01:38,660
You can then loop over it and access specific columns from each record by
26
00:01:38,660 --> 00:01:42,850
using the columns name as a property of that object.
27
00:01:43,140 --> 00:01:48,250
And of course, since this is a query builder, your queries can be more complex.
28
00:01:48,530 --> 00:01:54,160
We can use the where method to filter users based on some specific parameter.
29
00:01:54,420 --> 00:01:57,990
You can look up all of the available methods in the documentation,
30
00:01:58,190 --> 00:02:02,550
but we are not going to use DB façade in this course.
31
00:02:02,940 --> 00:02:06,660
There are cases where you would probably want to utilize it,
32
00:02:06,660 --> 00:02:09,259
but I will discuss them later in this lesson.
33
00:02:09,639 --> 00:02:15,550
In this course, I will use Eloquent ORM for managing database resources.
34
00:02:15,940 --> 00:02:19,450
Most of the popular MVC frameworks like Django,
35
00:02:19,450 --> 00:02:20,260
Flask,
36
00:02:20,270 --> 00:02:26,260
or Rails have some kind of implementation of the ORM for database interaction.
37
00:02:26,640 --> 00:02:30,720
ORM stands for object‑relational mapper.
38
00:02:30,930 --> 00:02:35,100
So what is the job of this Eloquent object‑relational mapper.
39
00:02:35,340 --> 00:02:39,810
Its main purpose is to map classes to database tables and the records or
40
00:02:39,810 --> 00:02:44,060
rows from those tables to the instances of that class.
41
00:02:44,440 --> 00:02:49,080
For example, in our application, we are working with the post resource.
42
00:02:49,370 --> 00:02:51,060
To store posts in the database,
43
00:02:51,060 --> 00:02:56,230
we'll probably create a table and name it post, and this table has
44
00:02:56,230 --> 00:02:59,960
some columns like the title and post description.
45
00:03:00,340 --> 00:03:01,430
So in this case,
46
00:03:01,430 --> 00:03:04,930
we can use Eloquent to create a Post class which
47
00:03:04,930 --> 00:03:07,840
will be mapped to this posts table,
48
00:03:08,090 --> 00:03:13,260
and each property of this class will be mapped to one of the table columns.
49
00:03:13,640 --> 00:03:15,500
Once this connection is established,
50
00:03:15,510 --> 00:03:19,890
we can use this mapped class to interact with the table from the database
51
00:03:19,900 --> 00:03:23,160
so that we can work with it in an object‑oriented way.
52
00:03:23,370 --> 00:03:26,660
This will work similarly to the Query Builder façade.
53
00:03:27,040 --> 00:03:27,930
For example,
54
00:03:27,940 --> 00:03:30,710
we can use the all static method to get the
55
00:03:30,710 --> 00:03:33,550
collection of all posts from the database.
56
00:03:33,940 --> 00:03:37,160
This returned collection contains all of the Post
57
00:03:37,160 --> 00:03:39,780
records in the form of objects.
58
00:03:40,060 --> 00:03:45,170
Each object is representing RO from the database so we can use object
59
00:03:45,170 --> 00:03:48,760
properties to get the value of the specific column.
60
00:03:49,040 --> 00:03:54,560
So let's say that I want to get the title of this second post from the database.
61
00:03:54,940 --> 00:03:59,360
I can use find method to get the specific post by ID,
62
00:03:59,660 --> 00:04:03,330
and once I have this post in the form of an object,
63
00:04:03,340 --> 00:04:06,250
I can use the title property to get the title.
64
00:04:06,640 --> 00:04:07,520
As you can see,
65
00:04:07,520 --> 00:04:11,080
this is much simpler than running all kinds of SQL queries and
66
00:04:11,080 --> 00:04:14,610
this abstraction allows us to work with permanent storage just
67
00:04:14,610 --> 00:04:17,160
like with any other object in PHP,
68
00:04:17,540 --> 00:04:22,060
and we usually refer to these ORM classes as models.
69
00:04:22,440 --> 00:04:26,030
So we would call this class the Post model.
70
00:04:26,260 --> 00:04:29,100
Now you have the full picture on why we say that
71
00:04:29,100 --> 00:04:32,130
Laravel follows the MVC architecture.
72
00:04:32,450 --> 00:04:37,150
We use views for presentation, controllers for business logic,
73
00:04:37,160 --> 00:04:40,650
and models for managing resources from the database.
74
00:04:40,940 --> 00:04:44,450
Okay, so now we know how to work with database resources,
75
00:04:44,450 --> 00:04:47,750
but what about the management of the database structure?
76
00:04:48,040 --> 00:04:52,120
What about creating tables or altering the structure of
77
00:04:52,130 --> 00:04:56,270
existing tables in the database? For that purpose,
78
00:04:56,270 --> 00:04:59,350
frameworks like Laravel use migrations.
79
00:04:59,740 --> 00:05:02,540
If you're familiar with GIT version control system,
80
00:05:02,540 --> 00:05:06,570
migrations would be like a versioning tool for your database.
81
00:05:06,670 --> 00:05:08,400
Just like with models,
82
00:05:08,410 --> 00:05:13,060
Laravel provides a way to manage table structure in regular PHP.
83
00:05:13,440 --> 00:05:19,460
Migrations are PHP classes with two required methods, up and down.
84
00:05:20,140 --> 00:05:24,590
Up method defines what the migration should do when we run it.
85
00:05:24,870 --> 00:05:28,930
We can use the schema façade to create tables, and we can define
86
00:05:28,930 --> 00:05:31,860
columns with this blueprint instance of the table.
87
00:05:32,940 --> 00:05:36,350
The down method should do the opposite of up.
88
00:05:36,360 --> 00:05:39,590
So if you ever try to roll back this migration,
89
00:05:39,600 --> 00:05:42,970
the table created in up method should be destroyed.
90
00:05:42,980 --> 00:05:46,760
We define what needs to be done to undo this migration.
91
00:05:47,140 --> 00:05:49,760
So for example, if you work in a team,
92
00:05:49,770 --> 00:05:54,200
every developer can just run these migration files and have the same
93
00:05:54,200 --> 00:05:57,660
structure in its own local development database.
94
00:05:58,040 --> 00:06:01,660
The same can be done for production or testing environment.
95
00:06:02,040 --> 00:06:05,470
So by using these models and migrations,
96
00:06:05,470 --> 00:06:09,520
we can manage the database structure and work with its data
97
00:06:09,530 --> 00:06:12,750
without ever typing a single line of SQL.
98
00:06:13,140 --> 00:06:15,010
Before we try this in practice,
99
00:06:15,020 --> 00:06:17,820
I want to make a quick note about the Query Builder.
100
00:06:18,090 --> 00:06:22,930
If we can use Eloquent models, why does this DB façade even exist?
101
00:06:23,210 --> 00:06:26,850
Even though we will only use models in this course,
102
00:06:26,860 --> 00:06:31,530
there are some situations in which you would want to utilize the DB façade.
103
00:06:31,840 --> 00:06:35,730
If you have some really complex queries and you are concerned
104
00:06:35,730 --> 00:06:38,760
about performance in a more substantial way,
105
00:06:38,760 --> 00:06:42,160
you can consider using the Query Builder because Eloquent
106
00:06:42,160 --> 00:06:44,650
sometimes can have a hit on performance,
107
00:06:44,940 --> 00:06:49,010
but for most applications, models are more than enough.
108
00:06:49,050 --> 00:06:49,750
Also,
109
00:06:49,760 --> 00:06:52,820
if you want to be independent from your models and
110
00:06:52,820 --> 00:06:57,260
work directly with some tables, you can do that with the DB façade.9512
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.