Thứ Bảy, 17 tháng 9, 2016

#Part6 laravel 5 Blog Tutorial : Create Single Page and 404 Page in laravel 5.3


Create blog using laravel 5.3 part6 - this lessons about how to create single page, create error 404 page, if an item/article not found that display the 404 page and make a link into title of our article in index page.

At the previews lessons, we have learned about displaying data from database, make a new form to create new data and save it into datase, validation, just read previews lessons :
  1. Insert Data, Validation, Redirect Page and Flash Message
  2. Model Eloquent & Displaying Data from Database

Create Single Page & 404 Page in laravel 5.3

on the our controller (BlogController.php) add one functions to show data into single page, and show the 404 page if data has not in the database.

Create Single Page and 404 Page in laravel 5.3

BlogController.php

public function show($id)
    {
        $blog = Blog::find($id);
        
        // return to 404 page
        if(!$blog){
          abort(404);
        }
        
        // display the article to single page
        return view('blog.detail')->with('blog',$blog);
    }

Next, create new file (detail.blade.php) in the our views, that file will be stored on /resources/views/blog/detail.blade.php

detail.blade.php

<h1>Detail Page</h1>
<h2>{{ $blog->title }}</h2>
<p>
  {{ $blog->description }}
</p>
<br>
<a href="/blog">Back to Home</a>

Next, to get your title shown as a link at home page, edit your index (index.blade.php) like this

index.blade.php

{{ Session::get('message') }}

<h1>My First Blog</h1>
@foreach ($blogs as $blog)
  <h2><a href="/blog/{{ $blog->id }}">{{ $blog->title }}</a></h2>
  <p>{{ $blog->description }}</p>
  <hr>
@endforeach

Video tutorial Create Single Page and 404 Page in laravel 5.3


See you next Lessons................

Không có nhận xét nào:

Đăng nhận xét