Article is a model. The articles.index is in the ArticlesController's index method. That method may show a view. A route resource controller generates the CRUD method stubs for you. You can make them all yourself but why do it when you can have the stubs generated.
Controller Questions
Hi,
Hi, I saw the tutorials online on how to create CRUD in laravel. Here is the link: http://itsolutionstuff.com/post/laravel-55-crud-example-from-scratchexample.html
I tried to understand and read the documentations of laravel but I can't really figure out what are these, could someone help me or guide me what's the use of these codes? Thanks in advance.
public function index()
{
$articles = Article::latest()->paginate(5);
return view('articles.index',compact('articles'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
- What is the
ArticlefromArticle::latest()is this Model name? - what is the
articlesfromcompact('articles')is that Route from web.php?Route::resource('articles','ArticlesController');
Thanks in advance.
@ejdelmonico did a good job explaining this but I will try to make it more simple.
-
Yes. "Article" here is the name of the model.
-
No.
compact('articles') is the variable from this line
$articles = Article::latest()->paginate(5);
Bascially, "compact('articles') is just saying to pass the variable $articles to the template.
view('articles.index',compact('articles'))
So you pass the variable $articles to the template located at resources/views/articles/index.
"view('articles.index'," uses dot notation which would be the '/articles/index" I just mentioned.
You were sort of close. When you asked about routes. But actually it was the location to the templates.
Route::resource('articles','ArticlesController');
here the 'articles' is part of the URL: http;//mywebsite.com/articles
ArticlesController should be self explanatory.
Please or to participate in this conversation.