Tự học Laravel: (14) MVC

Last time , I created sample data in the Articles table. This time we will display this information in Laravel.

So far, only the part that displays the view from the controller has been done, but this time the controller queries the model, and the model gets data from the DB and passes that data to the view for screen display. I will go through the flow of the MVC pattern .

ROUTING

Add the following two lines to web.php

// routes/web.php
 
Route::get('articles', 'ArticlesController@index');
Route::get('articles/{id}', 'ArticlesController@show');

{id} means to pass the id parameter to the show method.

DISPLAY ARTICLE LIST

CONTROLLER

Create ArticlesController.

php artisan make:controller ArticlesController

Add index and show methods to ArticlesController.

// app/Http/Controllers/ArticlesController.php
 
use App\Article;
 
class ArticlesController extends Controller {
 
    public function index() {
        $articles = Article::all();
 
        return view('articles.index', compact('articles'));
    }
 
    public function show($id) {
        return $id;
    }
}
  • index() extracts all the data in the Articles table and passes it to the view.
  • show() will only display the $ id received in the arguments for now.

VIEW

Create the articles.index view.

<!-- resouces/views/articles/index.blade.php -->
 
@extends('layout')
 
@section('content')
    <h1>Articles</h1>
    <hr/> 
    @foreach($articles as $article)
        <article>
            <h2>
                <a href="{{ url('articles', $article->id) }}">
                    {{ $article->title }}
                </a>
            </h2>
        </article>
    @endforeach
@endsection
  • It inherits the layout view created earlier .
  • I’m repeating writing articles one by one at @foreach.
  • The title of the article is a url helper function with a link to ‘articles/{id}’.

ACTION CONFIRMATION

First, start the server.

php artisan serve

Please access http://localhost:8000/articles and confirm that the article list is displayed. 
Click the article title to display the id.

ARTICLE DISPLAY

CONTROLLER

Implement the show method.

// app/Http/Controllers/ArticlesController.php
 
class ArticlesController extends Controller {
    // ... 
    public function show($id) {
        $article = Article::findOrFail($id);
 
        return view('articles.show', compact('article'));
    }
}

We use the findOfFail method to retrieve articles from Articles by id.

VIEW

Create the articles.show view.

<!-- resouces/views/articles/show.blade.php -->
 
@extends('layout')
 
@section('content')
    <h1>{{ $article->title }}</h1> 
    <hr/> 
    <article>
        <div class="body">{{ $article->body }}</div>
    </article>
@endsection

Access http://localhost:8000/articles and view the article list. 
Click on the title of the article and it will be completed if one article is displayed.

EXERCISE

  • Check out all() and findOrFail() in the Eloquent documentation. Also check out the other methods. http://laravel.com/docs/master/eloquent
  • Access http://localhost:8000/article/{id} with a non-existent id and check how it appears.

SUMMARY

I was able to understand the MVC pattern. 
It was possible to display the data of DB queried by model in the view. 
You can now use @foreach in views.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.