国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Laravel 5框架學習之日期,Mutator 和 Scope

2024-05-04 23:34:03
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Laravel 5框架學習之日期,Mutator 和 Scope的相關資料,需要的朋友可以參考下

在我們前面的解決方案中,直接給 published_at 賦值為當前日期實際上是一個臨時解決方案,我們需要設定發布日期,可能是未來2天后才發布,讓我們修改這個問題。

首先修改控制器:

 

 
  1. public function store() { 
  2. Article::create(Request::all()); 
  3. return redirect('articles'); 

然后修改視圖,添加發布日期字段

  1. @extends('layout'
  2.  
  3. @section('content'
  4. <h1>Write a New Article</h1> 
  5.  
  6. <hr/> 
  7.  
  8. {{--使用我們添加的 illuminate/html 開源庫--}} 
  9. {!! Form::open(['url' => 'articles']) !!} 
  10. <div class="form-group"
  11. {!! Form::label('title''Title:') !!} 
  12. {!! Form::text('title', null, ['class' => 'form-control']) !!} 
  13. </div> 
  14.  
  15. <div class="form-group"
  16. {!! Form::label('body''Body:') !!} 
  17. {!! Form::textarea('body', null, ['class' => 'form-control']) !!} 
  18. </div> 
  19.  
  20. <div class="form-group"
  21. {!! Form::label('published_at''Publish On:') !!} 
  22. {!! Form::input('date''published_at'date('Y-m-d'), ['class' => 'form-control']) !!} 
  23. </div> 
  24.  
  25. <div class="form-group"
  26. {!! Form::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!} 
  27. </div> 
  28.  
  29. {!! Form::close() !!} 
  30.  
  31. @stop 


ok,讓我們添加一個新的文章,并且把日期設置為未來的某一天,但是文章直接顯示在最開始了,這不是我們需要的。我們需要到了那天才顯示出來。當然,我們需要更具體一點,比如在早上 8:00 顯示,而不是0點顯示。我們可以添加一個mutator(也就是其他語言的屬性設置器),修改我們的model

  1. <?php namespace App; 
  2.  
  3. use DateTime; 
  4. use Illuminate/Database/Eloquent/Model; 
  5.  
  6. class Article extends Model { 
  7.  
  8. protected $fillable = [ 
  9. 'title'
  10. 'body'
  11. 'published_at' 
  12. ]; 
  13.  
  14. //屬性設置其要遵守格式約定 
  15. // set屬性Attribute 
  16. public function setPublishedAtAttribute($date) { 
  17. $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d'$date)->hour(8)->minute(0)->second(0); 
  18.  



添加一個新的紀錄,查看數據庫,我們已經將時間設置正確了,但是我們的首頁仍然顯示未來的才發布的文章,我們修改它。

  1. public function index() { 
  2. //$articles = Article::latest('published_at')->get(); 
  3. $articles = Article::latest('published_at')->where('published_at''<=', Carbon::now())->get(); 
  4.  
  5. return view('articles.index', compact('articles')); 


上面的解決方法可以工作,但是查詢語句太長了。我們可以使用 Laravel 提供的 scope ,來簡化我們的工作。所謂scope可以理解為是查詢過程中使用的中間查詢結果,比如我們定義一個published scope,他可以返回所有當前已經發布的文章,讓我們修改模型。

  1. //設置scope,遵守命名規則 
  2. public function scopePublished($query) { 
  3. $query->where('published_at''<=', Carbon::now()); 


修改控制器使用 scope

  1. public function index() { 
  2. //$articles = Article::latest('published_at')->get(); 
  3. //$articles = Article::latest('published_at')->where('published_at', '<=', Carbon::now())->get(); 
  4. $articles = Article::latest('published_at')->published()->get(); 
  5.  
  6. return view('articles.index', compact('articles')); 


結果相同,但在復雜的查詢中我們可以使用scope來分解我們的任務,或者復用查詢。

我們來增加一個新的查詢,查詢所有還沒有發布的文章。在模型中添加scope

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


修改控制器使用unpulished

 

 
  1. public function index() { 
  2. //$articles = Article::latest('published_at')->get(); 
  3. //$articles = Article::latest('published_at')->where('published_at', '<=', Carbon::now())->get(); 
  4. //$articles = Article::latest('published_at')->published()->get(); 
  5. $articles = Article::latest('published_at')->Unpublished()->get(); 
  6.  
  7. 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 是日期即可。

 

 
  1. protected $dates = ['published_at']; 
 

 

 

再次使用 dd($article->published_at->diffForHumans()); ,結果顯示為 3 days from now,Bingo!

以上所述就是本文的全部內容了,希望能夠給大家學習Laravel5框架有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东光县| 喀喇沁旗| 思南县| 连平县| 天津市| 大丰市| 无锡市| 武平县| 海盐县| 汉源县| 武义县| 辽阳市| 德州市| 孝昌县| 万盛区| 文昌市| 韶山市| 信阳市| 新沂市| 华宁县| 罗定市| 东阳市| 修文县| 本溪市| 盐边县| 乾安县| 蒙自县| 楚雄市| 水城县| 陕西省| 宜昌市| 资兴市| 仙游县| 建瓯市| 东安县| 花莲市| 樟树市| 汝南县| 宜良县| 广灵县| 土默特左旗|