laravel 数据库操作表、字段

1)创建表(make:migration create),例如创建 articles

php artisan make:migration create_articles_table --create=articles

运行命令后,会在 /database/migrations/ 生成对应的数据库迁移文件,通过修改文件里的 up 方法 和 down 文件,来创建数据表和删除数据表

::create('articles', (Blueprint ->increments('id'->('title',50->longText('content'->
::drop('articles'

运行 php artisan migrate 命令后,即可生效

PS:cretae 创建表时,字段要想得完善一些,后期不能修改这个文件了(修改或删除字段,需要新建一个数据库迁移文件,下面说)

详情的字段类型和操作,看这里 

命令 $table->bigIncrements('id');$table->bigInteger('votes');$table->binary('data');$table->boolean('confirmed');$table->char('name',4);$table->date('created_at');$table->dateTime('created_at');$table->dateTimeTz('created_at');$table->decimal('amount',5,2);$table->double('column',15,8);$table->enum('choices',['foo','bar']);$table->float('amount');$table->increments('id');$table->integer('votes');$table->ipAddress('visitor');$table->json('options');$table->jsonb('options');$table->longText('description');$table->macAddress('device');$table->mediumIncrements('id');$table->mediumInteger('numbers');$table->mediumText('description');$table->morphs('taggable');taggable_id 列和一个 STRING类型的 taggable_type列$table->nullableTimestamps();timestamps()一样但允许 NULL值.$table->rememberToken();remember_token 列: VARCHAR(100) NULL.$table->smallIncrements('id');$table->smallInteger('votes');$table->softDeletes();deleted_at 列 用于软删除.$table->string('email');$table->string('name',100);$table->text('description');$table->time('sunrise');$table->timeTz('sunrise');$table->tinyInteger('numbers');$table->timestamp('added_on');$table->timestampTz('added_on');$table->timestamps();created_at 和 updated_at列$table->timestampsTz();created_at 和 updated_at列(带时区)$table->unsignedBigInteger('votes');$table->unsignedInteger('votes');$table->unsignedMediumInteger('votes');$table->unsignedSmallInteger('votes');$table->unsignedTinyInteger('votes');$table->uuid('id'); 非空、默认值等修改操作看这里 

描述
修改器 ->after('column')->comment('my comment')->default($value)->first()->nullable()->storedAs($expression)->unsigned()integer 列为 UNSIGNED ->virtualAs($expression) 2)修改已创建的数据表字段(make:migration add)

想要修改已创建的数据表,不能直接改原来的 migrate 文件,要新建一个迁移文件,命令如下:

php artisan make:migration add_description_to_articles_table --table=articles
php artisan make:migration change_description_on_articles_table --table=articles

PS:其实migrate 文件的名字是怎么的都无所谓的,主要是里面的内容,不过名字都是要尽量写规范一点,让别人看到名字就知道是什么意思

添加或修改字段的操作是非常相似的,后者只是多了一个change()方法

新增字段:

::table('articles', (Blueprint ->('description')->nullable()->after('title'<span style="color: #0000ff">public <span style="color: #0000ff">function<span style="color: #000000"> down()
{
Schema
::table('articles',<span style="color: #0000ff">function
(Blueprint <span style="color: #800080">$table
<span style="color: #000000">) {
<span style="color: #800080">$table
->dropColumn('description'<span style="color: #000000">);
});
}

修改字段:

::table('articles', (Blueprint ->('description',200)-><span style="color: #0000ff">public <span style="color: #0000ff">function<span style="color: #000000"> down()
{
Schema
::table('articles',<span style="color: #0000ff">function
(Blueprint <span style="color: #800080">$table
<span style="color: #000000">) {
<span style="color: #008000">//
<span style="color: #000000"> });
}

运行 php artisan migrate 命令后,即可生效

3)使用索引

可用索引类型:

命令
$table->primary('id');$table->primary(['first','last']);$table->unique('email');$table->unique('state', 'my_index_name');$table->index('state'); 删除索引:

命令
$table->dropPrimary('users_id_primary');$table->dropUnique('users_email_unique');$table->dropIndex('geo_state_index'); 外键约束(references...on...):

Schema::table('posts', (->('user_id')->->foreign('user_id')->references('id')->on('users'

删除外键索引:

->dropForeign('user_id');

更详细的文档看这里:

相关文章

laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载
描述
描述
描述