使用phpMyAdmin建一個資料庫""chcsfs,或是下指令
# mysql -u root -p Enter password: mysql> CREATE DATABASE 'chcsfs'; Query OK, 1 row affected (0.00 sec)
二、編輯 .env
不要直接寫在 ./config/database.php,應寫在 .env
... DB_CONNECTION=mysql DB_HOST=localhost //這裡切記要用這個 DB_PORT=3306 DB_DATABASE=chcsfs DB_USERNAME=root DB_PASSWORD=密碼 ...
三、使用migrations建立migration表
若storage資料夾沒有設定777,可用sudo
php artisan migrate:install
四、migration指令
##建立遷移檔 php artisan make:migration create_posts_table ##順便建schema建一個資料表名稱為 posts php artisan make:migration create_posts_table2 --create=posts ##順便建schema改資料表名稱為 posts php artisan make:migration create_posts_table2 --table=posts ##右邊是4版前的指令,已不支援 php artisan migrate:make create_posts_table ##目前遷移檔的狀況 php artisan migrate:status ##會執行遷移檔中的 up()方法,依遷移檔建立、修改資料表 php artisan migrate ##會執行遷移檔中的 down()方法,用以還原或移除遷移檔的資料表 php artisan migrate:rollback
五、migrations遷移檔中schema的指令
Schema::create() ##建立資料表
Schema::rename($from,$to) ##改資料表名稱
Schema::talbe($from,$to) ##新增、修改該資表內的欄位
如:
https://laravel.com/docs/4.2/schema
public function up()
{
##建立資料表
Schema::create('posts', function($table){
$table->increments('id')->index(); ##並設定index
$table->boolean('confirmed');
$table->unsignedInteger('page_view');//正整數
$table->integer('page_view')->unsigned();//正整數2
$table->integer('votes');
$table->string('title');
$table->string('content');
$table->text('description');
$table->date('created_at');
$table->datetime('created_at');
$table->timestamps(); ##建立時間戳記
});
##在該資料表內,新增、修改、刪除欄位
Schema::table('posts', function($table){
$table-->dropColumn('欄位'); ##刪除欄位
$table->timestamps();
});
}
public function down()
{
Schema::drop('資料表')
Schema::dropIfExists('資料表')
}
沒有留言:
張貼留言