Laravel5 framework learning date, Mutator and Scope_php instances

Source: Internet
Author: User
This article mainly introduces the date of Laravel5 framework learning, Mutator and Scope. If you need more information, refer to our previous solutions, directly assigning published_at to the current date is actually a temporary solution. We need to set the release date, which may be released in the next two days. Let's modify this issue.

First, modify the controller:

  public function store() {    Article::create(Request::all());    return redirect('articles');  }

Modify the view and add the release date field.

@ Extends ('layout ') @ section ('content') Write a New Article
 {-- Use the illuminate \ html open source library we added --}}{!! Form: open (['url' => 'articles '])!}

{!! Form: label ('title', 'title :')!!} {!! Form: text ('title', null, ['class' => 'form-control'])!}

{!! Form: label ('body', 'body :')!!} {!! Form: textarea ('body', null, ['class' => 'form-control'])!}

{!! Form: label ('ed hed _ at', 'Publish On :')!!} {!! Form: input ('date', 'hhed _ at', date ('Y-m-d '), ['class' => 'form-control'])!}

{!! Form: submit ('add article', ['class' => 'btn btn-primary form-control'])!}

{!! Form: close ()!!} @ Stop

OK. Let's add a new article and set the date to one day in the future, but the article is directly displayed at the beginning, which is not what we need. We need to display it that day. Of course, we need to be more specific, for example, display at, instead. We can add a mutator (that is, the property setter of other languages) and modify our model.

<? Php namespace App; use DateTime; use Illuminate \ Database \ Eloquent \ Model; class Article extends Model {protected $ fillable = ['title', 'body ', 'hhed _ at']; // set attributes to comply with the format conventions // set Attribute public function setPublishedAtAttribute ($ date) {$ this-> attributes ['hhed _ at'] = Carbon: createFromFormat ('Y-m-d', $ date)-> hour (8) -> minute (0)-> second (0 );}}

Add a new record and view the database. We have set the time correctly, but our homepage still displays the articles published only in the future, and we modify it.

 public function index() {    //$articles = Article::latest('published_at')->get();    $articles = Article::latest('published_at')->where('published_at', '<=', Carbon::now())->get();    return view('articles.index', compact('articles'));  }

The preceding solution can work, but the query statement is too long. We can use the scope provided by Laravel to simplify our work. The so-called scope can be understood as the intermediate query results used in the query process. For example, if we define a published scope, it can return all the currently published articles and let us modify the model.

// Set scope and follow the naming rules public function scopePublished ($ query) {$ query-> where ('hhed _ at', '<=', Carbon :: now ());}

Modify controller to use scope

 public function index() {    //$articles = Article::latest('published_at')->get();    //$articles = Article::latest('published_at')->where('published_at', '<=', Carbon::now())->get();    $articles = Article::latest('published_at')->published()->get();    return view('articles.index', compact('articles'));  }

The results are the same, but in complex queries, we can use scope to break down our tasks, or reuse the query.

Let's add a new query to query all the articles that have not been published. Add scope to the model

  public function scopeUnpublished($query) {    $query->where('published_at', '>', Carbon::now());  }

Modify the Controller to use unpulished

 public function index() {    //$articles = Article::latest('published_at')->get();    //$articles = Article::latest('published_at')->where('published_at', '<=', Carbon::now())->get();    //$articles = Article::latest('published_at')->published()->get();    $articles = Article::latest('published_at')->Unpublished()->get();    return view('articles.index', compact('articles'));  }

One more thing! If we use dd ($ article-> published_at) in the show method to view the result, it is different from dd ($ article-> created_at). The former is our own field, the latter is automatically generated in CreateArticleTable through $ table-> timestamp. The automatically generated field is displayed as the Carbon type, while the string type is used. There are many benefits to using the Crabon type. For example, you can output dd ($ article-> created_at-> diffForHumans ();. This 1 hour ago result is not supported, but our published_at cannot. How to modify it? Modify the model and tell laravel that published_at is a date.

  protected $dates = ['published_at'];

Use dd ($ article-> published_at-> diffForHumans () again. The result is 3 days from now, Bingo!

The above is all the content of this article. I hope to help you learn about the Laravel5 framework.

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.