首頁  >  文章  >  後端開發  >  Laravel 5 基礎(十)- 日期,Mutator 和 Scope

Laravel 5 基礎(十)- 日期,Mutator 和 Scope

WBOY
WBOY原創
2016-08-08 09:26:45785瀏覽

在我們前面的解決方案中,直接給 published_at 賦值為當前日期實際上是一個臨時解決方案,我們需要設定發布日期,可能是未來2天后才發布,讓我們修改這個問題。

先修改控制器:

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

然後修改視圖,新增發布日期欄位

<code>@extends('layout')

@section('content')
    <h1>Write a New Article</h1>

    <hr/>

    {{--使用我们添加的 illuminate\html 开源库--}}
    {!! Form::open(['url' => 'articles']) !!}
        <div class="form-group">
            {!! Form::label('title', 'Title:') !!}
            {!! Form::text('title', null, ['class' => 'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('body', 'Body:') !!}
            {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('published_at', 'Publish On:') !!}
            {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!}
        </div>

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

@stop</code>

ok,讓我們新增一個新的文章,並且把日期設定為未來的某一天,但是文章直接顯示在最開始了,這不是我們需要的。我們需要到了那天才顯示出來。當然,我們需要更具體一點,例如在早上 8:00 顯示,而不是0點顯示。我們可以新增一個mutator(也就是其他語言的屬性設定器),修改我們的model

<code><?php namespace App;

use DateTime;
use Illuminate\Database\Eloquent\Model;

class Article extends Model {

	protected $fillable = [
        &#39;title&#39;,
        &#39;body&#39;,
        &#39;published_at&#39;
    ];

    //属性设置其要遵守格式约定
    // set属性Attribute
    public function setPublishedAtAttribute($date) {
        $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date)->hour(8)->minute(0)->second(0);
    }

}</code>

新增一個新的紀錄,查看資料庫,我們已經將時間設定正確了,但是我們的首頁仍然顯示未來的才發布的文章,我們修改它。

<code>	public function index() {
        //$articles = Article::latest('published_at')->get();
        $articles = Article::latest('published_at')->where('published_at', '<=&#39;, Carbon::now())->get();

        return view('articles.index', compact('articles'));
    }</code>

上面的解決方法可以工作,但是查詢語句太長了。我們可以使用 Laravel 提供的 scope ,來簡化我們的工作。所謂scope可以理解為是查詢過程中使用的中間查詢結果,例如我們定義一個published scope,他可以傳回所有目前已經發布的文章,讓我們修改模型。

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

修改控制器使用 scope

<code>	public function index() {
        //$articles = Article::latest(&#39;published_at&#39;)->get();
        //$articles = Article::latest('published_at')->where('published_at', '<=&#39;, Carbon::now())->get();
        $articles = Article::latest('published_at')->published()->get();

        return view('articles.index', compact('articles'));
    }</code>

結果相同,但在複雜的查詢中我們可以使用scope來分解我們的任務,或是複用查詢。

我們來增加一個新的查詢,查詢所有還沒發布的文章。在模型中加入scope

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

修改控制器使用unpulished

<code>	public function index() {
        //$articles = Article::latest('published_at')->get();
        //$articles = Article::latest('published_at')->where('published_at', '<=&#39;, Carbon::now())->get();
        //$articles = Article::latest('published_at')->published()->get();
        $articles = Article::latest('published_at')->Unpublished()->get();

        return view('articles.index', compact('articles'));
    }</code>

one more thing! 如果我們在show 方法中使用dd($article->published_at) 查看的結果,與dd($article->created_at); 結果不一樣,前者我們使我們自己的前者字段,後者是在CreateArticleTable 中透過$table->timestamp() 自動產生的。自動產生的欄位顯示出來是 Carbon類型,而我們的是字串。使用 Crabon類型有很多的好處,例如你可以輸出 dd($article->created_at->diffForHumans()); ,這種 1 hour ago 結果,但我們的published_at 不可以。怎麼修改?修改模型,告訴laravel,published_at 是日期即可。

<code>    protected $dates = ['published_at'];</code>

再使用 dd($article->published_at->diffForHumans()); ,結果顯示為 3 days from now,Bingo!

以上就介紹了Laravel 5 基礎(十)- 日期,Mutator 和 Scope,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn