[Laravel] basic database operations of Laravel, laravel database _ PHP Tutorial

Source: Internet
Author: User
[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...

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.