首页 > 其他 > 详细

laravel blog 二

时间:2015-09-14 00:32:20      阅读:232      评论:0      收藏:0      [点我收藏+]
  1. 修改数据库articles结构,添加到user表的外键,migrate:refresh
        public function up()
        {
            Schema::create(‘articles‘, function (Blueprint $table) {
                $table->increments(‘id‘);
                $table->integer(‘user_id‘)->unsigned();
                $table->string(‘title‘);
                $table->text(‘body‘);
                $table->timestamp(‘publishedAt‘);
                $table->timestamps();
                $table->foreign(‘user_id‘)
                      ->references(‘id‘)
                      ->on(‘users‘)
                      ->onDelete(‘cascade‘);
            });
        }

     

  2. model添加关系函数,一对多的关系,函数名可以自己定义。
    //user
        public function articles(){
            return $this->hasMany(‘App\article‘);
        }
    
    //article
        protected $fillable = [
            ‘title‘,
            ‘body‘,
            ‘publishedAt‘,
            ‘user_id‘   //临时添加字段
        ];
    
        public function user()
        {
            return $this->belongsTo(‘App\user‘);
        }

     

  3. 这个时候就可以在tinker里面添加数据,进行测试了。
  4. 提取view的edit,create的表单form,在form里临时添加字段
    {!! Form::hidden(‘user_id‘, 1) !!}

     

  5. route添加验证路由
    Route::controllers([
        ‘auth‘=>‘Auth\AuthController‘,
        ‘password‘=>‘Auth\PasswordController‘
        ]);

     

laravel blog 二

原文:http://www.cnblogs.com/fenle/p/4805941.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!