創建數據表
DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL COMMENT '姓名', `age` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '年齡', `sex` tinyint(3) unsigned NOT NULL DEFAULT '10' COMMENT '性別', `created_at` int(11) NOT NULL DEFAULT '0' COMMENT '新增時間', `update_at` int(11) NOT NULL DEFAULT '0' COMMENT '修改時間', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;連接數據庫
數據庫配置文件路徑laravel/config/database.php
//默認選擇數據庫 'default' => env('DB_CONNECTION', 'MySQL'), 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'passWord' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ],更改數據庫配置文件
env對應文件路徑laravel/ .env
DB_HOST=localhostDB_DATABASE=laravelDB_USERNAME=rootDB_PASSWORD=root使用DB facade實現CURD
新建StudentController.php控制器,代碼如下文件所在位置laravel/app/Http/Controllers/StudentController.php
<?php namespace App/Http/Controllers;use Illuminate/Support/Facades/DB;class StudentController extends Controller{ public function test1() { $students = DB::select('select * from student'); var_dump($students); }}配置路由,代碼如下
文件所在位置laravel/app/Http/routes.php
Route::get('test1',['uses'=>'StudentController@test1']);瀏覽器地址欄 http://localhost:8090/laravel/public/test1
頁面輸出:array(0) { }
說明數據庫連接成功。
新聞熱點
疑難解答