Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.