搜尋
首頁php教程php手册Laravel 5框架学习之日期,Mutator 和 Scope

这篇文章主要介绍了Laravel 5框架学习之日期,Mutator 和 Scope的相关资料,需要的朋友可以参考下

在我们前面的解决方案中,直接给 published_at 赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题。

首先修改控制器:

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

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

@extends('layout') @section('content')

Write a New Article


{{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open(['url' => 'articles']) !!}
{!! Form::label('title', 'Title:') !!} {!! Form::text('title', null, ['class' => 'form-control']) !!}
{!! Form::label('body', 'Body:') !!} {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
{!! Form::label('published_at', 'Publish On:') !!} {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
{!! Form::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!} @stop

ok,让我们添加一个新的文章,并且把日期设置为未来的某一天,但是文章直接显示在最开始了,这不是我们需要的。我们需要到了那天才显示出来。当然,,我们需要更具体一点,比如在早上 8:00 显示,而不是0点显示。我们可以添加一个mutator(也就是其他语言的属性设置器),修改我们的model

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', 'get(); return view('articles.index', compact('articles')); }

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

//设置scope,遵守命名规则 public function scopePublished($query) { $query->where('published_at', '

修改控制器使用 scope

public function index() { //$articles = Article::latest('published_at')->get(); //$articles = Article::latest('published_at')->where('published_at', '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', '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框架有所帮助。

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。