Laravel 5.2中關於.env檔與模型操作的實例分享

黄舟
發布: 2023-03-15 21:56:01
原創
1529 人瀏覽過

一、 .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- (預設)表示key 長32 位元字符,或 #AES-128

-CBC

 表示16 位元。 所以,為了確保會話和加密服務的安全, 

APP_KEY

 必須設置,使用Artisan 命令產生:

php artisan key:generate
登入後複製
這樣, .env 檔案中就會寫入一個新的 

APP_KEY

 。

 

二、模型運算

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() 函數類似print_r (), 用於列印變數訊息,是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

1.建立模型

#
php artisan make:model User
登入後複製

2. 表名、主鍵、時間戳記

表名:預設模型類別名稱的複數作為表名,可以在模型類別中定義protected $table 屬性來覆寫。

主鍵:預設主鍵名為"id",可以在模型類別中定義 protected $primaryKey 屬性來覆寫。

時間戳記:預設會管理 created_at 和 updated_a 字段,可以在模型類別中定義 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();
登入後複製

以上是Laravel 5.2中關於.env檔與模型操作的實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!