[Laravel5Fundamentals] 27-LooseEndsandWrappingUp

Source: Internet
Author: User
[Laravel5Fundamentals] 27-LooseEndsandWrappingUp sporadic tail scanning

Preface

This is the last section of the series. in this section, we will clean up all the fragmented issues.

Description

Development Environment: Windows 7

Laravel version: 5 +

IDE: Phpstorm

This section is the last part of this series. we will scan the last part of this series and break down the two fragmented questions ".

About Date

There are two questions about date:

1. when you edit an article, the name changes its date. However, when you save the modification, you can edit it again and find that the date is still the original date.

2. the date is always the date of the current day.

Let's take a look at the 2nd questions. open form. blade. php:

{!! Form::label('publish_at','Publish On:') !!}{!! Form::date('published_at',Carbon\Carbon::now(),['class'=>'form-control']) !!} 

From this we can see that the date of each article is determined by Carbon \ Carbon: now (). Naturally, it is the same day. Modify it to the following:

{!! Form::label('publish_at','Publish On:') !!}{!! Form::date('published_at',$article->published_at->format('Y-m-d'),['class'=>'form-control']) !!} 

It is not hard to understand. obtain the value of the published field of published_at from the $ article object and format it in Year-month-day format. Save and refresh the editing page. it looks like it is successful ~

Wait! When we create an article, an error is reported by using the create method. The reason is also very simple. when we create an article, this article object $ article does not exist at all, because it is still being created, and when we modify the article, this article object is created. What should we do?

Open the create. blade. php page that has referenced this form template, and modify it as follows:

{!! Form::model($article = new \App\Article, ['url'=>'articles']) !!} 

Here we will not use the open method anymore, but bind an article object with the model method. here, you can also see the article object, which is new and a new empty object. At this time, an error will still be reported when you create an article, because the format method cannot operate on null objects. Next, go to Article. php to edit a new method:

public function getPublishedAtAttribute($date){    return new Carbon($date);} 

The significance of this method is that when we want to obtain the value of the published_at field, if there is no value, return a new date, which is the current date.

Now, you can refresh the page for creating an article. everything is normal.

Tag cannot be clicked

Currently, the tag of an article can only be viewed but cannot be clicked. for example, a click will list all the articles with this tag. Next we will complete this point.

First, open routes. php and add a route:

Route::get('tags/{tag}','TagsController@show'); 

This means that when we access the/tags/tag uri, we will pass the tag to the show method of TagsController.

Create a TagsController. php using the command line:

phpartisanmake:controllerTagsController 

Open TagsController. php to create the show method:

public function show(Tag $tag){    return $tag;} 

Remember to add the use App \ Tag. open RouteServiceProvider. php, find the boot method, and add the following statement:

$router->bind('tags',function($name){    return \App\Tag::where('name',$name)->firstOrFail();}); 

It means binding the tags model to the route. the advantage is that laravel processes the logical relationships between these different layers and is transparent to you.

Now, you can view tags such as/tags/work or/tags/personal.

Well, we can see the tag information. However, we want to display the list of articles with this label.

Modify the show method of TagsController. php:

public function show(Tag $tag){    $articles = $tag->articles;    return view('articles.index',compact('articles'));} 

First, you can get the list of articles with this label, which coexist in the $ articles variable, and then return these article objects to the attempt layer. After saving the tag, visit the tab page again. The displayed list is the list of articles with the tag and the list of accessible articles.

Summary

This series provides some explanations on laravel 5.2, taking writing a blog as the practice direction. the application of laravel is explained in various layers and details of mvc. It involves many things, but it is not complicated. after all, it is still fundamental, but these basic points are worth exploring.

You are welcome to continue watching and learning this series. I am personally the first to come into contact with the php Framework. many understandings come from the java web framework. Although the languages are different, the architecture ideas are similar, all of them are based on mvc, but there are still some questions. You are welcome to come up and talk about them at the same time ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.