在我們前面的解決方案中,直接給 published_at 賦值為當(dāng)前日期實際上是一個臨時解決方案,我們需要設(shè)定發(fā)布日期,可能是未來2天后才發(fā)布,讓我們修改這個問題。
首先修改控制器:
public function store() { Article::create(Request::all()); return redirect('articles'); }然后修改視圖,添加發(fā)布日期字段
@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() !!}@stopok,讓我們添加一個新的文章,并且把日期設(shè)置為未來的某一天,但是文章直接顯示在最開始了,這不是我們需要的。我們需要到了那天才顯示出來。當(dāng)然,我們需要更具體一點,比如在早上 8:00 顯示,而不是0點顯示。我們可以添加一個mutator(也就是其他語言的屬性設(shè)置器),修改我們的model
<?php namespace App;use DateTime;use Illuminate/Database/Eloquent/Model;class Article extends Model {protected $fillable = [ 'title', 'body', 'published_at' ]; //屬性設(shè)置其要遵守格式約定 // set屬性Attribute public function setPublishedAtAttribute($date) { $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date)->hour(8)->minute(0)->second(0); }}添加一個新的紀(jì)錄,查看數(shù)據(jù)庫,我們已經(jīng)將時間設(shè)置正確了,但是我們的首頁仍然顯示未來的才發(fā)布的文章,我們修改它。
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可以理解為是查詢過程中使用的中間查詢結(jié)果,比如我們定義一個published scope,他可以返回所有當(dāng)前已經(jīng)發(fā)布的文章,讓我們修改模型。
//設(shè)置scope,遵守命名規(guī)則 public function scopePublished($query) { $query->where('published_at', '<=', Carbon::now()); }修改控制器使用 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')); }結(jié)果相同,但在復(fù)雜的查詢中我們可以使用scope來分解我們的任務(wù),或者復(fù)用查詢。
我們來增加一個新的查詢,查詢所有還沒有發(fā)布的文章。在模型中添加scope
public function scopeUnpublished($query) { $query->where('published_at', '>', Carbon::now()); }修改控制器使用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! 如果我們在 show 方法中使用 dd($article->published_at) 查看的結(jié)果,與 dd($article->created_at); 結(jié)果不一樣,前者我們使我們自己的字段,后者是在 CreateArticleTable 中通過 $table->timestamp() 自動生成的。自動生成的字段顯示出來是 Carbon類型,而我們的是字符串。使用 Crabon類型有很多的好處,例如你可以輸出 dd($article->created_at->diffForHumans()); ,這種 1 hour ago 結(jié)果,但我們的published_at 不可以。怎么修改?修改模型,告訴laravel,published_at 是日期即可。
protected $dates = ['published_at'];再次使用 dd($article->published_at->diffForHumans()); ,結(jié)果顯示為 3 days from now,Bingo!
新聞熱點
疑難解答