In our previous solution, assigning published_at directly to the current date is actually a temporary solution, and we need to set the release date, which is likely to be released 2 days from now, so let's revise the problem.
First modify the controller:
Public function Store () {
article::create (Request::all ());
Return redirect (' articles ');
}
Then modify the view to add the Publish Date field
@extends (' layout ') @section (' content ')
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 come up with that genius show. Of course, we need to be more specific, for example, at 8:00, rather than 0-point display. We can add a mutator (that is, the other language's property settings), modify our model
<?php namespace App;
Use DateTime;
Use Illuminate\database\eloquent\model;
Class Article extends Model {
protected $fillable = [
' title ',
' body ', '
published_at '
];
property to set it to conform to the Format Convention
//Set Property attribute public
function Setpublishedatattribute ($date) {
$this-> attributes[' published_at '] = Carbon::createfromformat (' y-m-d ', $date)->hour (8)->minute (0)->second (0);
}
}
Add a new record to view the database, we've set the time right, but our homepage still shows the future before the post is published 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 above workaround works, but the query statement is too long. We can simplify our work by using the scope provided by Laravel. Scope can be understood as the intermediate query results used in the query process, such as we define a published scope, he can return all the currently published articles, let us modify the model.
Set scope, follow the naming convention public
function scopepublished ($query) {
$query->where (' published_at ', ' <= ', Carbon: : Now ());
Modify controller 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 decompose our tasks, or to reuse queries.
Let's add a new query to query all articles that have not yet been published. Add scope to the model
Public Function scopeunpublished ($query) {
$query->where (' published_at ', ' > ', Carbon::now ());
}
Modify controller 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 we use DD ($article->published_at) to view the results in the Show method, with DD ($article->created_at); The result is not the same as the former we make our own fields, the latter is automatically generated by $table->timestamp () in createarticletable. The automatically generated fields are displayed as carbon types, while ours is a string. There are many benefits to using the Crabon type, for example, you can output DD ($article->created_at->diffforhumans ()); , this 1 hour ago results, but our published_at is not. How to modify? Modify the model to tell Laravel,published_at is the date.
protected $dates = [' published_at '];
Use DD again ($article->published_at->diffforhumans ()); , the results are shown as 3 days from now,bingo!
The above mentioned is the entire content of this article, hope to be able to give everybody to learn Laravel5 frame to help.