Laravel5中Mutator和Scope如何使用-创新互联

这期内容当中小编将会给大家带来有关Laravel 5中Mutator 和 Scope如何使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

创新互联专注于成都做网站、网站建设、网页设计、网站制作、网站开发。公司秉持“客户至上,用心服务”的宗旨,从客户的利益和观点出发,让客户在网络营销中找到自己的驻足之地。尊重和关怀每一位客户,用严谨的态度对待客户,用专业的服务创造价值,成为客户值得信赖的朋友,为客户解除后顾之忧。

首先修改控制器:

  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', '<=', Carbon::now())->get();

    return view('articles.index', compact('articles'));
  }

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

  //设置scope,遵守命名规则
  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'));
  }

结果相同,但在复杂的查询中我们可以使用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', '<=', 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) 查看的结果,与 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!

上述就是小编为大家分享的Laravel 5中Mutator 和 Scope如何使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


本文名称:Laravel5中Mutator和Scope如何使用-创新互联
本文路径:http://myzitong.com/article/djhspe.html

其他资讯