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:02,590
Now that we know how to insert data,
2
00:00:02,590 --> 00:00:06,860
it would be useful if we also knew how to retrieve it from the database.
3
00:00:07,240 --> 00:00:09,380
Just like with the Query Builder façade,
4
00:00:09,390 --> 00:00:12,850
we can chain methods to construct the specific query.
5
00:00:13,240 --> 00:00:14,070
For example,
6
00:00:14,070 --> 00:00:17,560
this will get all of the posts from the table where the title is
7
00:00:17,560 --> 00:00:21,100
new and it will order them by the title column.
8
00:00:21,360 --> 00:00:22,100
Finally,
9
00:00:22,100 --> 00:00:25,440
you should also add get at the end to actually
10
00:00:25,440 --> 00:00:27,660
retrieve the results from the database.
11
00:00:28,540 --> 00:00:31,960
You could also use the first method instead if you only need
12
00:00:31,960 --> 00:00:34,750
the first item from the returned collection.
13
00:00:35,240 --> 00:00:38,290
And these methods are actually the same methods we
14
00:00:38,290 --> 00:00:40,970
use on the Query Builder DB façade.
15
00:00:41,310 --> 00:00:43,730
Since this is the Laravel Fundamentals course,
16
00:00:43,730 --> 00:00:46,160
we will not use a lot of them in this project.
17
00:00:46,160 --> 00:00:49,140
So if you're interested in all of the available methods,
18
00:00:49,150 --> 00:00:53,260
check out the Query Builder section inside of the Laravel's documentation.
19
00:00:53,640 --> 00:00:54,600
At this moment,
20
00:00:54,610 --> 00:00:57,760
all we actually need to do is to retrieve all of the
21
00:00:57,760 --> 00:01:00,110
blog posts from the post table,
22
00:01:00,370 --> 00:01:05,850
and we can do that by simply calling the all method on the Model class itself.
23
00:01:06,240 --> 00:01:10,280
So this will actually return a collection of all posts.
24
00:01:10,360 --> 00:01:11,800
When I say collection,
25
00:01:11,810 --> 00:01:15,940
I'm actually referring to an instance of this Collection class from
26
00:01:15,940 --> 00:01:19,740
Eloquent which extends the basic Illuminate collection class we
27
00:01:19,740 --> 00:01:22,260
mentioned in one of the previous lessons.
28
00:01:22,540 --> 00:01:25,720
The special thing about this class is that you can chain
29
00:01:25,730 --> 00:01:29,260
other methods on top of this returned result.
30
00:01:29,540 --> 00:01:32,700
You can check out all of these methods in the documentation,
31
00:01:32,710 --> 00:01:35,560
find them under the Collections section.
32
00:01:35,940 --> 00:01:37,980
At the moment of recording,
33
00:01:37,990 --> 00:01:42,560
the Collections section is placed under the Digging Deeper concept.
34
00:01:44,340 --> 00:01:47,950
As you can see, there is a fair amount of methods here.
35
00:01:48,030 --> 00:01:51,150
You can use them to sort results, group them,
36
00:01:51,160 --> 00:01:56,460
implement joins, aggregation, and a lot of other useful functionality.
37
00:01:56,840 --> 00:01:59,890
Of course, since this is in the Digging Deeper section,
38
00:01:59,890 --> 00:02:02,780
we won't be covering these methods in this course,
39
00:02:02,780 --> 00:02:06,730
but you should know that they exist because you will probably have the need
40
00:02:06,730 --> 00:02:10,780
to at least order some collection in your own projects.
41
00:02:11,050 --> 00:02:15,650
Now that we know how to retrieve data, let's implement that in our application.
42
00:02:15,940 --> 00:02:21,060
I want to get all of the posts from the database and render them on the screen.
43
00:02:21,340 --> 00:02:24,800
We removed the Index section from the post controller
44
00:02:24,810 --> 00:02:28,060
because I wanted to do this in the home page.
45
00:02:28,240 --> 00:02:32,770
So let's import the Post model inside of the home controller,
46
00:02:33,060 --> 00:02:37,390
and now we can fetch the collection of all posts inside of
47
00:02:37,390 --> 00:02:40,560
the home action by using the all method.
48
00:02:41,040 --> 00:02:45,640
Hopefully, now we have all of the posts stored in this object,
49
00:02:45,820 --> 00:02:50,150
but how can we render these posts inside of the template?
50
00:02:50,440 --> 00:02:50,710
Well,
51
00:02:50,710 --> 00:02:56,580
it turns out that you can pass data to blade templates from this view function.
52
00:02:56,880 --> 00:03:01,460
This can be achieved by passing a simple array as a second parameter.
53
00:03:01,940 --> 00:03:06,460
The keys in this array will actually become variables which we can
54
00:03:06,460 --> 00:03:10,250
use inside of the template to refer to this data.
55
00:03:10,640 --> 00:03:15,760
In this case, I also named the key posts, but I didn't have to.
56
00:03:16,140 --> 00:03:19,460
These two components are not related by name.
57
00:03:20,140 --> 00:03:24,370
This is the name we will use in the template and this is
58
00:03:24,370 --> 00:03:27,060
the actual data assigned to that name.
59
00:03:28,110 --> 00:03:31,660
Now let's go to the home template to render this data on the screen.
60
00:03:34,240 --> 00:03:38,070
Since the key in the passed array was named posts,
61
00:03:38,080 --> 00:03:42,460
we can use that name as a variable here inside of the echo statement.
62
00:03:43,840 --> 00:03:45,540
Let's see what this looks like.
63
00:03:45,550 --> 00:03:50,690
Refresh the home page in the browser, and it seems that it works.
64
00:03:50,700 --> 00:03:53,550
We have the access from the blade template.
65
00:03:53,860 --> 00:03:56,840
Since we only have one post in the database,
66
00:03:56,850 --> 00:04:00,270
we can only see the properties of that post alone,
67
00:04:00,280 --> 00:04:02,800
but notice the square brackets.
68
00:04:03,100 --> 00:04:06,960
This is the principal representation of Eloquent collection.
69
00:04:07,340 --> 00:04:08,010
Of course,
70
00:04:08,020 --> 00:04:13,480
rendering data like this is not really user friendly so let's fix that.
71
00:04:13,780 --> 00:04:17,140
Instead of just putting this variable inside of an echo statement,
72
00:04:17,149 --> 00:04:21,050
let's instead loop through it with the foreach directive.
73
00:04:21,440 --> 00:04:24,380
Each post will be shown with this markup,
74
00:04:24,380 --> 00:04:28,140
the post‑content div inside of the post‑item div.
75
00:04:28,140 --> 00:04:32,300
These classes are already defined in the main.css file so
76
00:04:32,300 --> 00:04:34,300
just make sure that you add them here.
77
00:04:34,580 --> 00:04:35,350
And now,
78
00:04:35,350 --> 00:04:38,750
we can just echo the title and the description of each
79
00:04:38,750 --> 00:04:41,360
post‑item inside of the post‑content.
80
00:04:41,740 --> 00:04:45,580
Before we refresh the home page, let's create another post.
81
00:04:45,860 --> 00:04:49,090
I want to see what the home page will look like with
82
00:04:49,090 --> 00:04:51,550
more than one post in the database.
83
00:04:53,840 --> 00:04:55,760
Now let's click on Home.
84
00:04:57,640 --> 00:05:01,170
It seems like all of the posts are successfully rendered.
85
00:05:01,460 --> 00:05:03,900
The user experience is much better,
86
00:05:03,900 --> 00:05:06,820
and now we also know how to retrieve data from the
87
00:05:06,820 --> 00:05:09,550
database and pass it to the template.7348
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.