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


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

php artisan make:migration create_articles_table

運行命令后,會在 /database/migrations/ 生成對應的數據庫遷移文件,通過修改文件里的 up 方法 和 down 文件,來創建數據表和刪除數據表

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 命令后,即可生效

 

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

詳情的字段類型和操作,看這里 http://laravelacademy.org/post/6171.html#ipt_kb_toc_6171_8

命令 描述
$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_10

修改器 描述
->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)

想要修改已創建的數據表,不能直接改原來的 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()方法

新增字段:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM