laravel入门-mysql

1. 配置数据库

1 cat .env
2 
3 DB_CONNECTION=mysql
4 DB_HOST=127.0.0.1
5 DB_PORT=3306
6 DB_DATABASE=demo
7 DB_USERNAME=root
8 DB_PASSWORD=

2. 生成model

 1 2.1 生成model文件
 2     php artisan make:model User --migration
 3 
 4 2.2 修改migration文件
 5     cat 
 6    database/migrations/2018_12_08_105758_create_user_table.php
 7      
 8     Schema::create('users', function (Blueprint $table) {
 9             $table->increments('id');
10             $table->string("username",50);
11             $table->string("age",3);
12             $table->string("email",50);
13             $table->timestamps();
14         });
15 
16 2.3 refresh 表结构
17      php artisan migrate:refresh

3 显示哪些字段

 1 cat app/User.php
 2 
 3 <?php
 4 
 5 namespace App;
 6 
 7 use Illuminate\Database\Eloquent\Model;
 8 
 9 class User extends Model
10 {
11     //要显示哪些列
12     protected $fillable = [
13         'username','age','email'
14     ];
15 
16 
17 }

4 控制器里引入

 1 cat WelcomeController.php
 2 
 3 use App\User;  //引入User
 4 
 5 
 6     public function hello()
 7     {
 8         
 9         $user = User::find(1);  // id=1
10 
11         return $user ;
12     
13     }
14 
15 

5 增加路由

cat router/web.php
 
Route::get("/hello","welcomeController@hello");

参考文档: https://docs.golaravel.com/docs/5.0/eloquent/

猜你喜欢

转载自www.cnblogs.com/nika86/p/10090797.html
今日推荐