路由
Route::resource('articles','ArticleController');
model
class Article extends Model { //添加可填充的字段 protected $fillable = [ 'title', 'body', 'publishedAt' ]; //修改被提交的字段属性 public function setPublishedAtAttribute($date) { $this->attributes['publishedAt'] = Carbon::createFromFormat('Y-m-d', $date); } //自定义查询条件,在控制器中引用 public function scopePublished($query) { $query->where('publishedAt', '<=', Carbon::now()); } public function scopeUnPublished($query) { $query->where('publishedAt', '>', Carbon::now()); } }
处理请求,对提交的数据进行验证
<?php namespace App\Http\Requests; use App\Http\Requests\Request; class ArticleRequest extends Request { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'title' => 'required|min:3', 'body' => 'required', 'publishedAt' => 'required|date' ]; } }