zoukankan      html  css  js  c++  java
  • laravel blog 一

    路由

    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 AppHttpRequests;
    
    use AppHttpRequestsRequest;
    
    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'
            ];
        }
    }
    
  • 相关阅读:
    JAVA软件开发职责
    Redis主从复制配置
    VirtualBox安装Ubuntu教程
    分段锁——ConcurrentHashMap
    阻塞队列BlockingQueue用法
    阻塞队列--LinkedBlockingQueue
    MySQL百万级数据库优化方案
    获取主机的对外ip
    联通沃云开启80端口
    Nginx 正则匹配
  • 原文地址:https://www.cnblogs.com/fenle/p/4799715.html
Copyright © 2011-2022 走看看