在我们前面的解决方案中,直接给 published_at 赋值为当前日期实际上是一个临时解决方案,我们需要设定发布日期,可能是未来2天后才发布,让我们修改这个问题。
首先修改控制器:
rush:
PHP;">
public function store() {
Article::create(Request::all());
return redirect('articles');
}
然后修改视图,添加发布日期字段
rush:
PHP;">
@extends('layout')
@section('content')
Write a New Article
{{--使用我们添加的 illuminate\html 开源库--}}
{!! Form::open(['url' => 'articles']) !!}
<div class="form-group">
{!! Form::label('title','Title:') !!}
{!! Form::text('title',null,['class' => 'form-control']) !!}
<div class="form-group">
{!! Form::label('body','Body:') !!}
{!! Form::textarea('body',['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
rush:
PHP;">
use DateTime;
use Illuminate\Database\Eloquent\Model;
class Article extends Model {
protected $fillable = [
'title','body','published_at'
];
//属性设置其要遵守格式约定
// set属性Attribute
public function setPublishedAtAttribute($date) {
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d',$date)->hour(8)->minute(0)->second(0);
}
}
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'));
}
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'));
}