[Laravel 5 Fundamentals] 27–loose Ends and wrapping up

Source: Internet
Author: User

A few scraps of the mop

Objective

This section is the last section of the series, which we will take away some of the piecemeal issues.

Description

Development environment: Windows 7

Laravel version: 5+

Ide:phpstorm

This is the last of the series, and we sweep it off and make a "break" between the two problems.

Questions about the date

There are two questions about the date:

1, when you edit an article, the name modifies its date, but when you save the changes and then open the edit, the date is found or the original date.

2, the date always displays the date of the day.

Look at question 2nd first, open form.blade.php:

As you can see, the date of each piece of article is determined by Carbon\carbon::now (), Nature is the day. Modified to the following:

It is not difficult to understand, from $article object to get to published_at this article published the value of the field, and then formatted into Year-month-day format. Save and refresh the edit page, it seems to be successful.

Wait a minute! When we created an article, through the Create method, we found an error. The reason is also very simple, when we create the article, this article object $article does not exist at all, because it is still in the creation, and when we make changes to the article, this article object is already created. So what?

Open the create.blade.php page that references the form template, and modify the following:

Here we no longer use the Open method, but use the model method to bind a article object, here the article object you also see, is new out, new, is an empty object. Creating an article at this point will cause an error, because the format method cannot manipulate a null object. Next, let's go to article.php and edit a new method:

Public Function Getpublishedatattribute ($date) {    

The point of this method is that when we want to get the value of the Published_at field, if not, return a new date, and it is the current date.

This, you can refresh a page to create the article, everything, all, normal.

Problems with tags not clickable

The current article label can only be viewed without clicking, such as a click Will list all the articles holding the tag. Let's finish this point.

First, open routes.php and add a route:

This means that when we visit the URI of/tags/tag, we pass the tag to the Tagscontroller's Show method.

Then we create a tagscontroller.php from the command line:

Open tagscontroller.php to create the Show method:

Public function Show (Tag $tag) {    

Remember to add use App\tag; open routeserviceprovider.php below, locate the boot method, and add the following statement:

$router->bind (' tags ', function ($name) {    

It means that the tags model is bound to the route, and the benefit is that Laravel internally handles the logical relationships between these different layers, which is transparent to you.

At this point, you can visit the/tags/work or/tags/personal and other tags to see.

Well, yes, you can see the label information. But what we want is to display a list of articles that have that tag.

To modify the Show method for tagscontroller.php:

Public function Show (Tag $tag) {    $articles = $tag->articles;    

First get the list of articles that have the label, coexist in $articles this variable, and then return the article objects to the attempt layer. Once saved, visit the label page again, and the display should be a list of articles that hold the tag, and a list of articles that you can access.

Summarize

This series of Laravel 5.2 for some explanation, to write a blog for the practice direction, from the various layers of MVC, the details of the application of Laravel to say a bit. There are many things involved, but not complicated, after all, it is still fundamental, but these basic points are worth digging for.

Welcome you to watch and learn this series, I personally is the first time to contact the PHP framework, all have a lot of understanding is based on the Java web framework, although the language is different, but the architectural ideas are similar, are based on MVC, but there are certainly some problems, Welcome everyone can learn at the same time put forward and exchange ~

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