這篇文章主要介紹了Laravel 5框架學習之日期,Mutator 和 Scope的相關資料,需要的朋友可以參考下
在我們前面的解決方案中,直接給 published_at 賦值為當前日期實際上是一個臨時解決方案,我們需要設定發布日期,可能是未來2天后才發布,讓我們修改這個問題。
首先修改控制器:
- public function store() {
- Article::create(Request::all());
- return redirect('articles');
- }
然后修改視圖,添加發布日期字段
- @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);
- }
- }
添加一個新的紀錄,查看數據庫,我們已經將時間設置正確了,但是我們的首頁仍然顯示未來的才發布的文章,我們修改它。
- 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,他可以返回所有當前已經發布的文章,讓我們修改模型。
- //設置scope,遵守命名規則
- 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'));
- }
結果相同,但在復雜的查詢中我們可以使用scope來分解我們的任務,或者復用查詢。
我們來增加一個新的查詢,查詢所有還沒有發布的文章。在模型中添加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) 查看的結果,與 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!
以上所述就是本文的全部內容了,希望能夠給大家學習Laravel5框架有所幫助。
新聞熱點
疑難解答