改数据库相关配置。
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=test DB_USERNAME=root DB_PASSWORD=1
然后,找到router文件夹,web.php
开始处添加
use Illuminate\Support\Facades\DB;
然后,修改首页路由
Route::get ( '/', function () { $sql="select current_time()"; $time = DB::select($sql); var_dump( $time); // return view ( 'welcome' ); } );
刷新浏览器首页
array(1) { [0]=> object(stdClass)#191 (1) { ["current_time()"]=> string(8) "13:12:46" } }
看到上述浏览器结果,说明修改成功。
下面看迁移。
php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users
第一个命令是建表
第2个命令是改表。
其中,migration后面的词语是将要建立的迁移文件名,可以重复,因为laravel会自动加随机数区别文件名。
注意:上面两个命令只会帮你添加两个php的文件。
真的要改动数据库,需要在迁移文件加好后,执行
php artisan migrate
说明:
常用的主键型,int型,varchar型,说明如下
$table->increments('id'); //主键。 $table->string('name', 100)->default('')->comment('my comment'); //varchar $table->timestamps(); // 自动时间字段 $table->unsignedInteger('votes')->default(0)->comment('my comment'); // int型 $table->unsignedTinyInteger('votes')->default(0)->comment('my comment'); // tinyint型