Laravel 5.2 IV.. env application environment configuration and database operations, laravel. env
1. env file
The. env file is the environment configuration file of the application. It is used when you configure application parameters, database connections, and cache processing.
// APP_ENV = localAPP_DEBUG = true // APP_KEY = base64: plain/p9SatnV8m0kT4LVJR5w = // application keyAPP_URL = http: // localhost // database connection parameter DB_CONNECTION = mysqlDB_HOST = connector _ PORT = 3306DB_DATABASE = connector = rootDB_PASSWORD = DB_PREFIX = 'hd _ '// cache-related parameter CACHE_DRIVER = fileSESSION_DRIVER = fileQUEUE_DRIVER = sync // Redis connection parameter REDIS_HOST = 127.0.0.1REDIS _ PASSWORD = nullREDIS_PORT = 6379 // mail-related parameter MAIL_DRIVER = smtpMAIL_HOST = mailtrap. ioMAIL_PORT = 2525MAIL_USERNAME = nullMAIL_PASSWORD = nullMAIL_ENCRYPTION = null
To ensure the security of sessions and encrypted data, the APP_KEY must be set. You can use the Artisan command:
php artisan key:generate
2. Database Operations
2.1. DB native SQL query
// Insert DB: insert ('insert into hd_user (username, password) values (?, ?) ', ['Admin', 123456]); // query dd (DB: select ('select * from hd_user where username =? ', ['Admin']); // update DB: update ('Update hd_user set password =? Where username =? ', [654321, 'admin']); // delete DB: delete ('delete from hd_user where username =? ', ['Admin']);
Note: The dd () function is similar to print_r () and is used to print variable information. It is a helper function of Laravel.
2.2. Query Builder
The table method of the DB class returns a Query Builder for the given table.
// Query all databases: table ('user')-> get (); // query multiple databases: table ('user ') -> where ('age', '>', 20)-> get (); // query a database: table ('user ') -> where ('age', '>', 20)-> first (); // query a specific field DB: table ('user ') -> select ('name', 'email as user_email ')-> get (); // repeat the distinct () method $ users = DB: table ('user ') -> distinct ()-> get ();
2.3.Eloquent ORM
1. Create a model
We recommend that you use the Artisan command in the app or app/Http directory:
php artisan make:model User
2. Table Name, primary key, and timestamp
Table Name: by default, the plural model class name is used as the table name. You can define the protected $ table attribute in the model class to overwrite it.
Primary Key: the default primary key name is "id". You can define the protected $ primaryKey attribute in the model class to overwrite it.
Timestamp: The created_at and updated_a fields are managed by default. You can define the public $ timestamps attribute as false in the model class.
3. Data Operations
In the Controller method:
// Insert $ user = new User; $ user-> username = 'admin'; $ user-> save (); // query all users :: get (); // query multiple users: where ('age', '>', '20')-> get (); // query a User :: find (1); // update $ user = User: find (1); // query a record with a primary key of 1 $ user-> username = 'new name '; $ user-> save (); // or use the update () method // Delete // method 1. first retrieve the record and then delete User: find (1)-> delete (); // method 2. directly delete User: destroy (1, 2) through the primary key; // method 3. use the where condition to delete User: where ('username', 'admin')-> delete ();