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{
public function up(){
Schema::table('movies', function(Blueprint $table){
$table->string('movie_title', 150);
})
}
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');
$table->timestamps();
php artisan migrate:reset 重置所有表
转载请注明原文地址: https://ju.6miu.com/read-1296982.html