[Laravel] Basic database Operations Section of Laravel

Source: Internet
Author: User

[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

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.