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

在咱们前面的解决方案中,直接给 published_at 赋值为当前日期其实是一个临时解决方案,咱们须要设定发布日期,多是将来2天后才发布,让咱们修改这个问题。php

首先修改控制器:html

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

而后修改视图,添加发布日期字段laravel

@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

ok,让咱们添加一个新的文章,而且把日期设置为将来的某一天,可是文章直接显示在最开始了,这不是咱们须要的。咱们须要到了那天才显示出来。固然,咱们须要更具体一点,好比在早上 8:00 显示,而不是0点显示。咱们能够添加一个mutator(也就是其余语言的属性设置器),修改咱们的model数据库

<?php namespace App;

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

class Article extends Model {

    protected $fillable = [
        'title',
        'body',
        'published_at'
    ];

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

}

添加一个新的纪录,查看数据库,咱们已经将时间设置正确了,可是咱们的首页仍然显示将来的才发布的文章,咱们修改它。this

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'));
    }

上面的解决方法能够工做,可是查询语句太长了。咱们可使用 Laravel 提供的 scope ,来简化咱们的工做。所谓scope能够理解为是查询过程当中使用的中间查询结果,好比咱们定义一个published scope,他能够返回全部当前已经发布的文章,让咱们修改模型。url

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

修改控制器使用 scopespa

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'));
    }

结果相同,但在复杂的查询中咱们可使用scope来分解咱们的任务,或者复用查询。code

咱们来增长一个新的查询,查询全部尚未发布的文章。在模型中添加scopeorm

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

修改控制器使用unpulishedhtm

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! 若是咱们在 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 是日期便可。

protected $dates = ['published_at'];

再次使用 dd($article->published_at->diffForHumans()); ,结果显示为 3 days from now,Bingo!

相关文章
相关标签/搜索