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

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

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

首先修改控制器:

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

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

@extends('layout')

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

  <hr/>

  {{--使用我們添加的 illuminate\html 開(kāi)源庫(kù)--}}
  {!! 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è)置為未來(lái)的某一天,但是文章直接顯示在最開(kāi)始了,這不是我們需要的。我們需要到了那天才顯示出來(lái)。當(dāng)然,我們需要更具體一點(diǎn),比如在早上 8:00 顯示,而不是0點(diǎn)顯示。我們可以添加一個(gè)mutator(也就是其他語(yǔ)言的屬性設(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ù)庫(kù),我們已經(jīng)將時(shí)間設(shè)置正確了,但是我們的首頁(yè)仍然顯示未來(lái)的才發(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'));
  }

上面的解決方法可以工作,但是查詢(xún)語(yǔ)句太長(zhǎng)了。我們可以使用 Laravel 提供的 scope ,來(lái)簡(jiǎn)化我們的工作。所謂scope可以理解為是查詢(xún)過(guò)程中使用的中間查詢(xún)結(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ù)雜的查詢(xún)中我們可以使用scope來(lái)分解我們的任務(wù),或者復(fù)用查詢(xún)。

我們來(lái)增加一個(gè)新的查詢(xún),查詢(xún)所有還沒(méi)有發(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 中通過(guò) $table->timestamp() 自動(dòng)生成的。自動(dòng)生成的字段顯示出來(lái)是 Carbon類(lèi)型,而我們的是字符串。使用 Crabon類(lèi)型有很多的好處,例如你可以輸出 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如何使用了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

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

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

廣告

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

微信小程序開(kāi)發(fā)