All language subtitles for 5.passing-data-to-blade-templates

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now that we know how to insert data,

it would be useful if we also knew how to retrieve it from the database.

Just like with the Query Builder façade,

we can chain methods to construct the specific query.

For example,

this will get all of the posts from the table where the title is

new and it will order them by the title column.

Finally,

you should also add get at the end to actually

retrieve the results from the database.

You could also use the first method instead if you only need

the first item from the returned collection.

And these methods are actually the same methods we

use on the Query Builder DB façade.

Since this is the Laravel Fundamentals course,

we will not use a lot of them in this project.

So if you're interested in all of the available methods,

check out the Query Builder section inside of the Laravel's documentation.

At this moment,

all we actually need to do is to retrieve all of the

blog posts from the post table,

and we can do that by simply calling the all method on the Model class itself.

So this will actually return a collection of all posts.

When I say collection,

I'm actually referring to an instance of this Collection class from

Eloquent which extends the basic Illuminate collection class we

mentioned in one of the previous lessons.

The special thing about this class is that you can chain

other methods on top of this returned result.

You can check out all of these methods in the documentation,

find them under the Collections section.

At the moment of recording,

the Collections section is placed under the Digging Deeper concept.

As you can see, there is a fair amount of methods here.

You can use them to sort results, group them,

implement joins, aggregation, and a lot of other useful functionality.

Of course, since this is in the Digging Deeper section,

we won't be covering these methods in this course,

but you should know that they exist because you will probably have the need

to at least order some collection in your own projects.

Now that we know how to retrieve data, let's implement that in our application.

I want to get all of the posts from the database and render them on the screen.

We removed the Index section from the post controller

because I wanted to do this in the home page.

So let's import the Post model inside of the home controller,

and now we can fetch the collection of all posts inside of

the home action by using the all method.

Hopefully, now we have all of the posts stored in this object,

but how can we render these posts inside of the template?

Well,

it turns out that you can pass data to blade templates from this view function.

This can be achieved by passing a simple array as a second parameter.

The keys in this array will actually become variables which we can

use inside of the template to refer to this data.

In this case, I also named the key posts, but I didn't have to.

These two components are not related by name.

This is the name we will use in the template and this is

the actual data assigned to that name.

Now let's go to the home template to render this data on the screen.

Since the key in the passed array was named posts,

we can use that name as a variable here inside of the echo statement.

Let's see what this looks like.

Refresh the home page in the browser, and it seems that it works.

We have the access from the blade template.

Since we only have one post in the database,

we can only see the properties of that post alone,

but notice the square brackets.

This is the principal representation of Eloquent collection.

Of course,

rendering data like this is not really user friendly so let's fix that.

Instead of just putting this variable inside of an echo statement,

let's instead loop through it with the foreach directive.

Each post will be shown with this markup,

the post‑content div inside of the post‑item div.

These classes are already defined in the main.css file so

just make sure that you add them here.

And now,

we can just echo the title and the description of each

post‑item inside of the post‑content.

Before we refresh the home page, let's create another post.

I want to see what the home page will look like with

more than one post in the database.

Now let's click on Home.

It seems like all of the posts are successfully rendered.

The user experience is much better,

and now we also know how to retrieve data from the

database and pass it to the template.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.