Laravel 5框架學(xué)習(xí)之日期,Mutator 和 Scope
在我們前面的解決方案中,直接給 published_at 賦值為當(dāng)前日期實(shí)際上是一個(gè)臨時(shí)解決方案,我們需要設(shè)定發(fā)布日期,可能是未來2天后才發(fā)布,讓我們修改這個(gè)問題。
首先修改控制器:
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() !!}
@stop
ok,讓我們添加一個(gè)新的文章,并且把日期設(shè)置為未來的某一天,但是文章直接顯示在最開始了,這不是我們需要的。我們需要到了那天才顯示出來。當(dāng)然,我們需要更具體一點(diǎn),比如在早上 8:00 顯示,而不是0點(diǎn)顯示。我們可以添加一個(gè)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);
}
}
添加一個(gè)新的紀(jì)錄,查看數(shù)據(jù)庫,我們已經(jīng)將時(shí)間設(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é)果,比如我們定義一個(gè)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ù)用查詢。
我們來增加一個(gè)新的查詢,查詢所有還沒有發(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! 如果我們?cè)?show 方法中使用 dd($article->published_at) 查看的結(jié)果,與 dd($article->created_at); 結(jié)果不一樣,前者我們使我們自己的字段,后者是在 CreateArticleTable 中通過 $table->timestamp() 自動(dòng)生成的。自動(dòng)生成的字段顯示出來是 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!
以上所述就是本文的全部內(nèi)容了,希望能夠給大家學(xué)習(xí)Laravel5框架有所幫助。
- 異步 HttpContext.Current實(shí)現(xiàn)取值的方法(解決異步Application,Session,Cache...等失效的問題)
- 瀏覽器關(guān)閉使session失效的問題多種解決方式
- iframe跨域與session失效問題的解決辦法
- Laravel 5框架學(xué)習(xí)之Eloquent (laravel 的ORM)
- Laravel 5框架學(xué)習(xí)之表單
- Laravel 5框架學(xué)習(xí)之表單驗(yàn)證
- Laravel 5 框架入門(一)
- 關(guān)于擴(kuò)展 Laravel 默認(rèn) Session 中間件導(dǎo)致的 Session 寫入失效問題分析
- Session對(duì)象失效的客戶端解決方法
相關(guān)文章
微信公眾號(hào)OAuth2.0網(wǎng)頁授權(quán)問題淺析
根據(jù)需求,我今天完成的是微信的網(wǎng)頁授權(quán)然后拉取用戶的一些基本信息的問題。具體內(nèi)容詳情大家通過本文學(xué)習(xí)吧2017-01-01
PHP頁面跳轉(zhuǎn)實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)的方法
下面小編就為大家?guī)硪黄狿HP頁面跳轉(zhuǎn)實(shí)現(xiàn)延時(shí)跳轉(zhuǎn)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
phpinfo 系統(tǒng)查看參數(shù)函數(shù)代碼
并根據(jù)自身的理解做了很多修改和優(yōu)化,就當(dāng)前而言,這是探測(cè)信息最全面的PHP探針了!2009-06-06
Zend Framework框架的數(shù)據(jù)庫追蹤器使用示例
這篇文章主要介紹了Zend Framework框架的數(shù)據(jù)庫追蹤器使用示例,現(xiàn)把追蹤器的使用方法分享給大家,需要的朋友可以參考下2014-03-03

