Laravel 5 Basics (10)-date, Mutator and Scope

Source: Internet
Author: User
Tags compact
In our previous solution, assigning the 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 2 days, let's change the issue.

First modify the controller:

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

Then modify the view to add the Release date field

@extends('layout')@section('content')    

Write a New Article

{{--使用我们添加的 illuminate\html 开源库--}} {!! 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('published_at', 'Publish On:') !!} {!! Form::input('date', 'published_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 a future day, but the article appears directly at the beginning, which is not what we need. We need to show that genius. Of course, we need to be more specific, such as at 8:00, instead of the 0-point display. We can add a mutator (that is, a property set for other languages) and modify our model


  
   attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date)->hour(8)->minute(0)->second(0);    }}

Add a new record to view the database, we have set the time correctly, but our homepage still shows the future only post the article, 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 workaround above works, 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 result used in the query process, such as we define a published scope, he can return all the currently published articles, let us modify the model.

    //设置scope,遵守命名规则    public function scopePublished($query) {        $query->where('published_at', '<=', Carbon::now());    }

Modifying the controller using 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 decompose our tasks, or reuse queries.

Let's add a new query and query all the articles that haven't been published yet. Add scope to the model

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

Modifying the controller using 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 the results of the view in the show method dd($article->published_at) , unlike dd($article->created_at); the results, the former we make our own fields, which are CreateArticleTable $table->timestamp() generated automatically in the. The automatically generated field is displayed as a Carbon type, and ours is a string. There are many benefits to using the Crabon type, such as you can output dd($article->created_at->diffForHumans()); , 1 hour ago but we published_at can't. How to modify? Modify the model and tell Laravel that published_at it is the date.

    protected $dates = ['published_at'];

Re-use dd($article->published_at->diffForHumans()); , the results are shown as 3 days from now , bingo!

The above describes the Laravel 5 Foundation (10)-date, Mutator and Scope, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.