Laravel5中Mutator和Scope如何使用-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Laravel 5中Mutator 和 Scope如何使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)專注于成都做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

首先修改控制器:

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

然后修改視圖,添加發(fā)布日期字段

@extends('layout')

@section('content')
  <h2>Write a New Article</h2>

  <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! 如果我們在 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!

上述就是小編為大家分享的Laravel 5中Mutator 和 Scope如何使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁名稱:Laravel5中Mutator和Scope如何使用-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://bm7419.com/article34/djhspe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、微信小程序、網(wǎng)站改版、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)