laravel 數據庫操做(表、字段)

1)建立表(make:migration create),例如建立 articlesphp

php artisan make:migration create_articles_table

運行命令後,會在 /database/migrations/ 生成對應的數據庫遷移文件,經過修改文件裏的 up 方法 和 down 文件,來建立數據表和刪除數據表html

public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->string('title',50); $table->longText('content'); $table->timestamps(); }); }
public function down() { Schema::drop('articles'); }

運行 php artisan migrate 命令後,便可生效laravel

 

PS:cretae 建立表時,字段要想得完善一些,後期不能修改這個文件了(修改或刪除字段,須要新建一個數據庫遷移文件,下面說)數據庫

詳情的字段類型和操做,看這裏 http://laravelacademy.org/post/6171.html#ipt_kb_toc_6171_8express

命令 描述
$table->bigIncrements('id'); 自增ID,類型爲bigint
$table->bigInteger('votes'); 等同於數據庫中的BIGINT類型
$table->binary('data'); 等同於數據庫中的BLOB類型
$table->boolean('confirmed'); 等同於數據庫中的BOOLEAN類型
$table->char('name', 4); 等同於數據庫中的CHAR類型
$table->date('created_at'); 等同於數據庫中的DATE類型
$table->dateTime('created_at'); 等同於數據庫中的DATETIME類型
$table->dateTimeTz('created_at'); 等同於數據庫中的DATETIME類型(帶時區)
$table->decimal('amount', 5, 2); 等同於數據庫中的DECIMAL類型,帶一個精度和範圍
$table->double('column', 15, 8); 等同於數據庫中的DOUBLE類型,帶精度, 總共15位數字,小數點後8位.
$table->enum('choices', ['foo', 'bar']); 等同於數據庫中的 ENUM類型
$table->float('amount'); 等同於數據庫中的 FLOAT 類型
$table->increments('id'); 數據庫主鍵自增ID
$table->integer('votes'); 等同於數據庫中的 INTEGER 類型
$table->ipAddress('visitor'); 等同於數據庫中的 IP 地址
$table->json('options'); 等同於數據庫中的 JSON 類型
$table->jsonb('options'); 等同於數據庫中的 JSONB 類型
$table->longText('description'); 等同於數據庫中的 LONGTEXT 類型
$table->macAddress('device'); 等同於數據庫中的 MAC 地址
$table->mediumIncrements('id'); 自增ID,類型爲無符號的mediumint
$table->mediumInteger('numbers'); 等同於數據庫中的 MEDIUMINT類型
$table->mediumText('description'); 等同於數據庫中的 MEDIUMTEXT類型
$table->morphs('taggable'); 添加一個 INTEGER類型的 taggable_id 列和一個 STRING類型的 taggable_type
$table->nullableTimestamps(); 和 timestamps()同樣但容許 NULL值.
$table->rememberToken(); 添加一個 remember_token 列: VARCHAR(100) NULL.
$table->smallIncrements('id'); 自增ID,類型爲無符號的smallint
$table->smallInteger('votes'); 等同於數據庫中的 SMALLINT 類型
$table->softDeletes(); 新增一個 deleted_at 列 用於軟刪除.
$table->string('email'); 等同於數據庫中的 VARCHAR 列  .
$table->string('name', 100); 等同於數據庫中的 VARCHAR,帶一個長度
$table->text('description'); 等同於數據庫中的 TEXT 類型
$table->time('sunrise'); 等同於數據庫中的 TIME類型
$table->timeTz('sunrise'); 等同於數據庫中的 TIME 類型(帶時區)
$table->tinyInteger('numbers'); 等同於數據庫中的 TINYINT 類型
$table->timestamp('added_on'); 等同於數據庫中的 TIMESTAMP 類型
$table->timestampTz('added_on'); 等同於數據庫中的 TIMESTAMP 類型(帶時區)
$table->timestamps(); 添加 created_at 和 updated_at
$table->timestampsTz(); 添加 created_at 和 updated_at列(帶時區)
$table->unsignedBigInteger('votes'); 等同於數據庫中無符號的 BIGINT 類型
$table->unsignedInteger('votes'); 等同於數據庫中無符號的 INT 類型
$table->unsignedMediumInteger('votes'); 等同於數據庫中無符號的 MEDIUMINT 類型
$table->unsignedSmallInteger('votes'); 等同於數據庫中無符號的 SMALLINT 類型
$table->unsignedTinyInteger('votes'); 等同於數據庫中無符號的 TINYINT 類型
$table->uuid('id'); 等同於數據庫的UUID

非空、默認值等修改操做看這裏 http://laravelacademy.org/post/6171.html#ipt_kb_toc_6171_10json

修改器 描述
->after('column') 將該列置於另外一個列以後 (僅適用於MySQL)
->comment('my comment') 添加註釋信息
->default($value) 指定列的默認值
->first() 將該列置爲表中第一個列 (僅適用於MySQL)
->nullable() 容許該列的值爲NULL
->storedAs($expression) 建立一個存儲生成列(只支持MySQL)
->unsigned() 設置 integer 列爲 UNSIGNED
->virtualAs($expression) 建立一個虛擬生成列(只支持MySQL)

 

2)修改已建立的數據表字段(make:migration add)post

想要修改已建立的數據表,不能直接改原來的 migrate 文件,要新建一個遷移文件,命令以下:ui

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

PS:其實migrate 文件的名字是怎麼的都無所謂的,主要是裏面的內容,不過名字都是要儘可能寫規範一點,讓別人看到名字就知道是什麼意思spa

添加或修改字段的操做是很是類似的,後者只是多了一個change()方法code

新增字段:

public function up() { Schema::table('articles', function (Blueprint $table) { $table->string('description')->nullable()->after('title'); }); } public function down() { Schema::table('articles', function (Blueprint $table) { $table->dropColumn('description'); }); }

修改字段:

public function up() { Schema::table('articles', function (Blueprint $table) { $table->string('description', 200)->change(); }); } public function down() { Schema::table('articles', function (Blueprint $table) { //  }); }

運行 php artisan migrate 命令後,便可生效

 

3)使用索引

可用索引類型:

命令 描述
$table->primary('id'); 添加主鍵索引
$table->primary(['first', 'last']); 添加混合索引
$table->unique('email'); 添加惟一索引
$table->unique('state', 'my_index_name'); 指定自定義索引名稱,若是不指定,laravel會自動給它起個名字
$table->index('state'); 添加普通索引

刪除索引:

命令 描述
$table->dropPrimary('users_id_primary'); 從 「users」表中刪除主鍵索引
$table->dropUnique('users_email_unique'); 從 「users」表中刪除惟一索引
$table->dropIndex('geo_state_index'); 從 「geo」表中刪除普通索引

 

外鍵約束(references...on...):

Schema::table('posts', function ($table) { $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); });

刪除外鍵索引:

$table->dropForeign('user_id');

 

 

更詳細的文檔看這裏:http://laravelacademy.org/post/6171.html

相關文章
相關標籤/搜索