Laravel小记

1.创建表:

php artisan migrate:make create_movies_table

目录app/database/migrations/下生成一个表文件,它有一个up()和down()函数,down()是up的反向操作,比如说创建字段 > 移除字段

2.创建字段

public function up()
{
   Scheme::create('movies', function($table){
        $table->incretments('moive_id');
   });
}

public function down()
{
   Schema::drop('movies');
}

3.生成表

php artisan migrate

回滚操作

php artisan migrate:rollback

4.创建表,并执行Schema的操作

php artisan migrate:make create_reviews_table --create=reviews

app/database/migrations/下多了个表文件

class CreateReviewsTable extends Migration {

  /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('reviews', function(Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::drop('reviews');
    }


}

执行php artisan migrate

5.修改表,添加字段

php artisan migrate:make add_movie_title_to_movies_table --table=movies

执行后

class AddMovieTitleToMoviesTable extends Migration{
     /**
     *  Run the migrations.
     *  @return void
     */
     public function up(){
       Schema::table('movies', function(Blueprint $table){
            $table->string('movie_title', 150);//长度150
       })

     }

      /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down() {
        Schema::table('movies', function(Blueprint $table)
            $table->dropColumn('movie_title');//自己添加
        );
    }



}

6.添加数据

<?php
class MovieTableSeeder extends Seeder
{
   public function run()
   {
       $data = array(
         array('movie_title' => '魔戒'),
         array('movie_tile' => '冰与火'),
         array('movie_tile' => '龙珠')
       );
       DB:table('movies')->insert($data);
   }

}
<?php
class DatabaseSeeder extends Seeder {
    /**
    * Run the database seeds
    * @return void 
    */
    public function run()
    {
        Eloquent::unguard();
        $this->call('MovieTableSeeder');
     }

}

执行php artisan db:seed

7.添加常用字段

$table->increments('movie_id');//设置主键
$table->string('movie_title');
$table->text('movie_content');
$table->integer('movie_budget')->unsigned();
$table->data('movie_date')->default('0000-00-00');
$table->primary('movie_id');//设置主键,与increments一样,不可重用
$table->timestamps();

php artisan migrate:reset
重置所有表

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值