[Laravel] Laravel the database configuration
Locate the. env file under the program directory structure
To configure basic database connection information
db_host=127.0.0.1
db_port=3306
Db_database=blog
Db_username=root
Db_password=root
You need to restart the service when you finish modifying the. env file
[Laravel] Laravel Getting Started with databases
Import DB database Operation class in controller, use DB
Use the DB Class static method Select to query the database, Db::select (), Parameters: SQL statement, array of parameter values
For example: $user =db::select ("select * from article where id=?", Array ("1"));
Gets an array of every result in the array that is a Stdclass object
<?phpnamespace App\http\controllers\index; UseApp\http\controllers\controller; UseDB;classIndexcontrollerextendscontroller{ Public functionindex () {$data=Array(); $data[' title ']= ' Index controller '; //First Kind $user=db::select ("SELECT * from article where id=?",Array("1")); foreach($user as $v) { Echo $v-title; } //The second Kind $users=db::table ("article")get (); foreach($user as $v) { Echo $v-title; } returnView ("Index.index",$data); }}
Using the Query Builder
Using Db::table (), get the query constructor object, parameters: Table name
Call the Get () method of the Builder object to get the array data
For example: $users =db::table ("article")->get ();
The query constructor is a chained call, and there are other ways to view the document
[Laravel] Migration of databases
Create a migration with the artisan command, make:migration name –create table name
Example: PHP artisan make:migration create_users_table--create=users
This command creates a migration file under the Database/migrations directory
Open the generated migration file and create the field in the up method, which will use the database's structure constructor schema
Run the migration command, using the command PHP artisan migrate, which automatically creates the table in the database
[Laravel] Eloquent Model
Using the eloquent model to establish a mapping model for the table ORM, using the artisan command Make:model the model name
Example: PHP artisan Make:model User
Generate a user.php model file in the app directory
[Laravel] Laravel Basic Database Operations Section