[Laravel] basic database operations of Laravel, laravel database. [Laravel] basic database operations of Laravel. find the program directory structure in the database configuration of laravel database [laravel] laravel. env file configuration basic database connection information D [Laravel] basic database operations section of Laravel, laravel database
[Laravel] laravelDatabase configuration
Find the. env file under the program directory structure
Configure basic database connection information
DB_HOST = 127.0.0.1
DB Port = 3306
DB_DATABASE = blog
DB_USERNAME = root
DB_PASSWORD = root
After modifying the. env file, restart the service.
[Laravel] laravelDatabase entry
Database operations imported into the controller, use DB
Use the static select method of the DB class to query the database, DB: select (), parameter: SQL statement, parameter value array
Example: $ user = DB: select ("select * from article where id =? ", Array (" 1 "));
Returns an array. each result in the array is a StdClass object.
Title;} // The second type $ users = DB: table ("article")-> get (); foreach ($ user as $ v) {echo $ v-> title;} return view ("index. index ", $ data );}}
Use the query constructor
Use DB: table () to obtain the query constructor object. parameter: table name.
Call the get () method of the Builder object to obtain the array data.
Example: $ users = DB: table ("article")-> get ();
The query constructor is called in a chain. you can view the document for other methods.
[Laravel]Database Migration
Use the Artisan command to create a migration. make: migration name-create table name
Example: php artisan make: migration create_users_table -- create = users
This command will create a migration file under the database/migrations Directory
Open the generated migration file and create fields in the up method. the Schema of the database is used here.
Run the migration command and run the php artisan migrate command to automatically create a table in the database.
[Laravel] EloquentModel
Use the Eloquent model to create a orm ing model ORM for the table, and use the Artisan command make: model name
Example: php artisan make: model User
Generate a User. php model file in the app Directory
In the basic database operations section of mongolaravel, find the database configuration of the Laravel database [laravel] laravel under the program directory structure. configure the basic database connection information in the env file D...