Laravel 5框架学习之日期,Mutator 和 Scope
在我们前面的解决方案中,直接给published_at赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题。
首先修改控制器:
publicfunctionstore(){
Article::create(Request::all());
returnredirect('articles');
}
然后修改视图,添加发布日期字段
@extends('layout')
@section('content')
<h1>WriteaNewArticle</h1>
<hr/>
{{--使用我们添加的illuminate\html开源库--}}
{!!Form::open(['url'=>'articles'])!!}
<divclass="form-group">
{!!Form::label('title','Title:')!!}
{!!Form::text('title',null,['class'=>'form-control'])!!}
</div>
<divclass="form-group">
{!!Form::label('body','Body:')!!}
{!!Form::textarea('body',null,['class'=>'form-control'])!!}
</div>
<divclass="form-group">
{!!Form::label('published_at','PublishOn:')!!}
{!!Form::input('date','published_at',date('Y-m-d'),['class'=>'form-control'])!!}
</div>
<divclass="form-group">
{!!Form::submit('AddArticle',['class'=>'btnbtn-primaryform-control'])!!}
</div>
{!!Form::close()!!}
@stop
ok,让我们添加一个新的文章,并且把日期设置为未来的某一天,但是文章直接显示在最开始了,这不是我们需要的。我们需要到了那天才显示出来。当然,我们需要更具体一点,比如在早上8:00显示,而不是0点显示。我们可以添加一个mutator(也就是其他语言的属性设置器),修改我们的model
<?phpnamespaceApp;
useDateTime;
useIlluminate\Database\Eloquent\Model;
classArticleextendsModel{
protected$fillable=[
'title',
'body',
'published_at'
];
//属性设置其要遵守格式约定
//set属性Attribute
publicfunctionsetPublishedAtAttribute($date){
$this->attributes['published_at']=Carbon::createFromFormat('Y-m-d',$date)->hour(8)->minute(0)->second(0);
}
}
添加一个新的纪录,查看数据库,我们已经将时间设置正确了,但是我们的首页仍然显示未来的才发布的文章,我们修改它。
publicfunctionindex(){
//$articles=Article::latest('published_at')->get();
$articles=Article::latest('published_at')->where('published_at','<=',Carbon::now())->get();
returnview('articles.index',compact('articles'));
}
上面的解决方法可以工作,但是查询语句太长了。我们可以使用Laravel提供的scope,来简化我们的工作。所谓scope可以理解为是查询过程中使用的中间查询结果,比如我们定义一个publishedscope,他可以返回所有当前已经发布的文章,让我们修改模型。
//设置scope,遵守命名规则
publicfunctionscopePublished($query){
$query->where('published_at','<=',Carbon::now());
}
修改控制器使用scope
publicfunctionindex(){
//$articles=Article::latest('published_at')->get();
//$articles=Article::latest('published_at')->where('published_at','<=',Carbon::now())->get();
$articles=Article::latest('published_at')->published()->get();
returnview('articles.index',compact('articles'));
}
结果相同,但在复杂的查询中我们可以使用scope来分解我们的任务,或者复用查询。
我们来增加一个新的查询,查询所有还没有发布的文章。在模型中添加scope
publicfunctionscopeUnpublished($query){
$query->where('published_at','>',Carbon::now());
}
修改控制器使用unpulished
publicfunctionindex(){
//$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();
returnview('articles.index',compact('articles'));
}
onemorething!如果我们在show方法中使用dd($article->published_at)查看的结果,与dd($article->created_at);结果不一样,前者我们使我们自己的字段,后者是在CreateArticleTable中通过$table->timestamp()自动生成的。自动生成的字段显示出来是Carbon类型,而我们的是字符串。使用Crabon类型有很多的好处,例如你可以输出dd($article->created_at->diffForHumans());,这种1hourago结果,但我们的published_at不可以。怎么修改?修改模型,告诉laravel,published_at是日期即可。
protected$dates=['published_at'];
再次使用dd($article->published_at->diffForHumans());,结果显示为3daysfromnow,Bingo!
以上所述就是本文的全部内容了,希望能够给大家学习Laravel5框架有所帮助。