Laravel5 basics (10)-date, Mutator and Scope in our previous solution, assigning published_at values directly to the current date is actually a temporary solution. we need to set the release date, it may be released in the next two days. let's modify this issue. First modify the controller: publicfunctionstore () {Article: create (Laravel 5 base (10)-date, Mutator, and Scope
In our previous solution, assigning the value of published_at to the current date is actually a temporary solution. we need to set the release date, which may be released in the next two days, let's modify this question.
First, modify the controller:
public function store() { Article::create(Request::all()); return redirect('articles'); }
Modify the view and add the release date field.
@ Extends ('layout ') @ section ('content') Write a New Article
{-- Use the illuminate \ html open source library we added --}}{!! 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 ('Ed Hed _ at', 'publish On :')!!} {!! Form: input ('date', 'hhed _ at', date ('Y-m-D '), ['class' => 'form-control'])!}
{!! Form: submit ('add article', ['class' => 'btn btn-primary form-control'])!}
{!! Form: close ()!!} @ Stop
OK. Let's add a new article and set the date to one day in the future, but the article is directly displayed at the beginning, which is not what we need. We need to display it that day. Of course, we need to be more specific, for example, display at, instead. We can add a mutator (that is, the property setter of other languages) and modify our model.
Attributes ['Ed Hed _ at'] = Carbon: createFromFormat ('Y-m-D', $ date)-> hour (8)-> minute (0) -> second (0 );}}
Add a new record and view the database. we have set the time correctly, but our homepage still displays the articles published only in the future, and we modify it.
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')); }
The preceding solution can work, but the query statement is too long. We can use the scope provided by Laravel to simplify our work. The so-called scope can be understood as the intermediate query results used in the query process. for example, if we define a published scope, it can return all the currently published articles and let us modify the model.
// Set scope and follow the naming rules public function scopePublished ($ query) {$ query-> where ('hhed _ at', '<=', Carbon :: now ());}
Modify controller to use 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')); }
The results are the same, but in complex queries, we can use scope to break down our tasks, or reuse the query.
Let's add a new query to query all the articles that have not been published. Add scope to the model
public function scopeUnpublished($query) { $query->where('published_at', '>', Carbon::now()); }
Modify the controller to use 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! If weshow
Use in methoddd($article->published_at)
View results, anddd($article->created_at);
The results are different. for the former, we use our own fields, and for the latterCreateArticleTable
Passed$table->timestamp()
Automatically generated. Automatically generated fields are displayedCarbon
Type, while we are strings. There are many benefits to using the Crabon type, for example, you can outputdd($article->created_at->diffForHumans());
1 hour ago
Results, but ourpublished_at
No. How to modify it? Modify the model and tell laravel,published_at
Is the date.
protected $dates = ['published_at'];
Use Againdd($article->published_at->diffForHumans());
, The result is displayed3 days from now
, Bingo!