Example sharing about .env files and model operations in Laravel 5.2

黄舟
Release: 2023-03-15 21:56:01
Original
1558 people have browsed it

1. .env file

.env file is the environment configuration file of the application. This file will be used when configuring application parameters, database connection, and cache processing .


// 应用相关参数
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  
Copy after login

Among them, regarding the explanation of this APP_KEY, there are the following comments in the config/app.php file:


/*
|--------------------------------------------------------------------------
| 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',
Copy after login

key key reads the APP_KEY of the .env file, which is usually a 32-bit random string. The cipher key determines the length of APP_KEY, which is generally AES-256-CBC (default) indicating that the key is 32 characters long, or AES-128-CBC means 16 bits.

So, in order to ensure the security of the session and encryption service, APP_KEY must be set, use the Artisan command to generate:


php artisan key:generate
Copy after login

In this way, A new APP_KEY will be written to the .env file.

2. Model operation

1. DB class


// 插入
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']);
Copy after login

Note: dd() function is similar to print_r (), used to print variable information, is an auxiliary function of Laravel.

2. Query Builder

The table method of the DB class returns a query builder for the given 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();
Copy after login

 

3. Eloquent ORM

1. Create model


php artisan make:model User
Copy after login

2. Table name, primary key, timestamp

Table name: The plural of the default model class name is used as the table name, which can be overridden by defining the protected $table attribute in the model class.

Primary key: The default primary key name is "id", which can be overridden by defining the protected $primaryKey attribute in the model class.

Timestamp: created_at and updated_a fields will be managed by default. You can define the public $timestamps attribute as false in the model class.

3. Data operation

In the controller method:


// 插入
$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();
Copy after login

The above is the detailed content of Example sharing about .env files and model operations in Laravel 5.2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!