Laravel 5.2 四、.env 文件與模型操作


一、.env文件

.env 文件是應用的環境配置文件,在配置應用參數、數據庫連接、緩存處理時都會使用這個文件。

// 應用相關參數
APP_ENV=local
APP_DEBUG=true   //應用調試模式
APP_KEY=base64:hMYz0BMJDJARKgrmaV93YQY/p9SatnV8m0kT4LVJR5w= //應用key
APP_URL=http://localhost

// 數據庫連接參數
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelblog
DB_USERNAME=root
DB_PASSWORD=
DB_PREFIX='hd_'

// 緩存相關參數
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

// Redis 連接參數
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

// 郵件相關參數
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null  

其中,有關這個 APP_KEY 的解釋,在 config/app.php 中有如下注釋:

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('APP_KEY'),

'cipher' => 'AES-256-CBC',

key 鍵讀取 .env 文件的 APP_KEY ,一般是 32 位的隨機字符串。cipher 鍵決定 APP_KEY 的長度,一般是 AES-256-CBC (默認)表示 key 長 32 位字符,或者 AES-128-CBC 表示 16 位。

所以,為了保證會話和加密服務的安全, APP_KEY 必須設置,使用 Artisan 命令生成:

php artisan key:generate

這樣,.env 文件中就會寫入一個新的 APP_KEY 。

 

二、模型操作

Laravel 提供了 DB 類、查詢構建器和 Elequent 模型這三個工具來實現數據庫操作。

 

1. DB類

// 插入
DB::insert('insert into hd_user(username, password) values(?, ?)', ['admin', 123456]);

// 查詢
DB::select('select * from hd_user where username = ?', ['admin']);

// 更新
DB::update('update hd_user set password= ? where username = ?', [654321, 'admin']);

// 刪除
DB::delete('delete from hd_user where username = ?', ['admin']); 

注:dd() 函數用於打印變量的詳細信息,是 Laravel 的輔助函數。

 

2. 查詢構建器

DB 類的 table 方法為給定表返回一個查詢構建器。

// 查詢所有
DB::table('user')->get();

// 查詢多條
DB::table('user')->where('age', '>', 20)->get();

// 查詢一條
DB::table('user')->where('age', '>', 20)->first();

// 查詢特定字段
DB::table('user')->select('name', 'email as user_email')->get();

// distinct() 方法去重復
$users = DB::table('user')->distinct()->get();

   

3. Eloquent ORM

迷人的ORM,Laravel 的 Elequent 模型提供了簡潔的數據庫操作。比查詢構建器更對象化,封裝性更高。功能不如查詢構建器強大,但可以使用查詢構建器的方法。

1.創建模型

php artisan make:model User

2. 表名、主鍵、時間戳

表名:默認模型類名的復數作為表名,可以在模型類中定義 protected $table 屬性來覆蓋。

主鍵:默認主鍵名為"id",可以在模型類中定義 protected $primaryKey 屬性來覆蓋。

時間戳:默認會管理 created_at 和 updated_at 字段,可以在模型類中定義 public $timestamps 屬性為 false 取消。

3.數據操作

在控制器方法中:

// 插入
$user = new User;
$user->username = 'admin';
$user->save();


// 查詢

// 查詢所有
User::get();

// 查詢多條
User::where('age', '>', '20')->get();

// 查詢一條
user::find(1);


// 更新
$user = User::find(1); // 查找主鍵為1的一條記錄
$user->username = 'new name';
$user->save();         // 或者用 update() 方法


// 刪除

// 方法1.先獲取記錄再刪除
User::find(1)->delete();

// 方法2.通過主鍵直接刪除
User::destroy(1, 2);

// 方法3.通過 where 條件刪除
User::where('username', 'admin')->delete();

 


免責聲明!

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



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