archiebango's avatar

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);
    }
  1. What is the Article from Article::latest() is this Model name?
  2. what is the articles from compact('articles') is that Route from web.php? Route::resource('articles','ArticlesController');

Thanks in advance.

0 likes
7 replies
ejdelmonico's avatar

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.

1 like
martinbean's avatar

@archiebango Might be worth reading the Laravel documentation if you don’t understand what’s going on in that brief code snippet.

robrogers3's avatar

You need to sign up to LARACASTS!!!! You'll be an expert in no time! Or at least pretty sufficient. Best money you'll ever spend.

SapporoGuy's avatar
Level 2

@ejdelmonico did a good job explaining this but I will try to make it more simple.

  1. Yes. "Article" here is the name of the model.

  2. 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.

1 like

Please or to participate in this conversation.