Laravel 5.2 example sharing about. env files with model operations

Source: Internet
Author: User

I. ENV file

An. env file is an app's environment profile that is used when configuring application parameters, database connections, and cache processing .


Apply relevant parameters App_env=localapp_debug=true//   application Debug mode app_key=base64:hmyz0bmjdjarkgrmav93yqy/p9satnv8m0kt4lvjr5w=// Apply keyapp_url=http://localhost//Database connection parameters db_connection=mysqldb_host=127.0.0.1db_port=3306db_database= laravelblogdb_username=rootdb_password=db_prefix= ' hd_ '//cache related parameters Cache_driver=filesession_driver=filequeue_driver =sync//Redis Connection Parameters redis_host=127.0.0.1redis_password=nullredis_port=6379//message related parameters mail_driver=smtpmail_host= Mailtrap.iomail_port=2525mail_username=nullmail_password=nullmail_encryption=null

Among them, the explanation about this App_key , in the config/app.php file has the following comment:


/*|--------------------------------------------------------------------------| Encryption key|--------------------------------------------------------------------------| | This key was used by the illuminate Encrypter service and should be set| To a random, character string, otherwise these encrypted strings| Won't is safe. Before deploying an application!| */' key ' = env (' App_key '), ' cipher ' = ' AES-256-CBC ',

The key key reads the App_key of the. env file, typically a 32-bit random string. The cipher key determines the length of the App_key , typically aes--CBC (the default) denotes a key length of 32 characters, or aes- -CBC represents 16 bits.

Therefore, to ensure the security of the session and cryptographic Services, the App_key must be set and generated using the Artisan command:


PHP Artisan Key:generate

In this way, a new App_key is written to the. env file.

Second, model operation

1. DB class


Insert Db::insert (' INSERT into hd_user (username, password) VALUES (?,?) ', [' admin ', 123456]);//Query Db::select (' SELECT * FROM Hd_user where username =? ', [' admin ']);//Update db::update (' Update hd_user set password=? where Username =? ', [654321, ' Ad Min ']);//delete db::d elete (' delete from hd_user where username =? ', [' admin ']);

Note: the DD () function is similar to Print_r (), which is used to print variable information and is an auxiliary function of Laravel.

2. Query Builder

The table method of the DB class returns a query builder for the given table.


Query all db::table (' user ')->get ();//Query multiple db::table (' user ')->where (' Age ', ' > ')->get ();//Query a db::table (' user ')->where (' Age ', ' > ', '->first ');//Query specific field db::table (' user ')->select (' name ', ' email as User_ Email ')->get ()//Distinct () method to repeat $users = db::table (' user ')->distinct ()->get ();

  

3. Eloquent ORM

1. Create a model


PHP Artisan Make:model User

2. Table name, primary key, timestamp

Table name: The complex number of the default model class name is used as the table name and can be overridden by defining protected $table properties in the Model class.

Primary key: The default primary key name is "id" and can be overridden by defining the protected $primaryKey property in the Model class.

Timestamp: The Created_at and Updated_a fields are managed by default and can be defined in the Model class $timestamps property is False.

3. Data manipulation

In the Controller method:


//Insert $user = new User; $user->username = ' admin '; $user->save ();//Query// Query all User::get ();//Query multiple User::where (' age ', ' > ', ' 1 ')->get ();//Query a user::find ();//Update $user = User::find (1);         Find a record with primary key 1 $user->username = ' new name '; $user->save (); or use the update () method//delete//Method 1. First get the record and then delete user::find (1)->delete ();//Method 2. Delete user directly via primary key::d Estroy (1, 2);//Method 3. Through where Conditional delete user::where (' username ', ' admin ')->delete (); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.