Home >PHP Framework >Laravel >laravel database settings
Laravel is an open source PHP web framework that is excellent at processing data. Laravel provides a simple, flexible, and easy-to-use ORM (Object Relational Mapping) method, making it more convenient for developers to deal with different databases.
When using Laravel, we need to set up the database link first so that Laravel can correctly access our database. Below we will explain how to set up the database in Laravel.
In Laravel, we can set our database information by modifying the .env file. We can find the following information in the .env file:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
DB_CONNECTION
is used to specify the type of database. The database types supported by Laravel include mysql, pgsql, sqlite, sqlsrv, etc. DB_HOST
is used to specify the host name or IP address where the database is located, generally designated as localhost
or 127.0.0.1
. DB_PORT
is used to specify the port number of the database server. DB_DATABASE
is used to specify the database name to use. DB_USERNAME
is used to specify the user name used to connect to the database. DB_PASSWORD
is used to specify the password used to connect to the database. After completing the above settings, Laravel will use these settings to connect to our database.
Laravel provides the database migration function, which can facilitate us to migrate data between different databases. What needs to be noted when performing database migration is that we need to create the database first and set up the corresponding connection information, and then use the migrator to migrate the data.
In Laravel, we can create a migration file by executing the php artisan make:migration create_users_table
command. This command will generate a new migration file in the database/migrations
directory, with a file name similar to 2019_04_01_000001_create_users_table.php
.
After creating the migration file, we need to open the file and edit the up
method and down
method. Among them, the up
method will be called when executing migration to define the database operations we need to perform; the down
method will be called when undoing migration to define our The undo operation that needs to be performed. Let's take creating a user table as an example to demonstrate the code:
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }
The above code will create a table named users
, which contains 5 fields id
, name
, email
, password
and remember_token
, as well as two automatically maintained fields created_at
and updated_at
.
After completing the above settings, we can execute the php artisan migrate
command to perform data migration operations.
In Laravel, we can use Eloquent ORM to conveniently operate our database. Eloquent ORM provides many methods for performing CRUD (create, read, update, delete) operations, which can help us quickly perform database operations.
Let’s first look at how to set up the database in the model. In the model class, we can use the following methods to specify the table name, primary key and database connection information:
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; class User extends Model { protected $table = 'users'; protected $primaryKey = 'id'; protected $connection = 'mysql'; }
The above code will specify the use of mysql
connection to access users
Table, the primary key of this table is id
.
After setting the database connection information, we can use Eloquent ORM to perform database operations. Let's look at some basic operations of Eloquent ORM.
In Eloquent ORM, we can use the create
method to create data. For example:
$user = User::create([ 'name' => 'Tom', 'email' => 'tom@example.com', 'password' => bcrypt('password'), ]);
The above code will create a user named Tom
, email address is tom@example.com
, and password is password
data.
In Eloquent ORM, we can use the get
method to query data. For example:
$users = User::get();
The above code will query all user data from the users
table.
We can also use the where
method to perform conditional queries. For example:
$users = User::where('name', 'Tom')->get();
The above code will query all user data named Tom
from the users
table.
In Eloquent ORM, we can use the update
method to update data. For example:
$user = User::where('name', 'Tom')->first(); $user->email = 'new_email@example.com'; $user->save();
The above code will change the email address of the user data named Tom
to new_email@example.com
.
In Eloquent ORM, we can use the delete
method to delete data. For example:
$user = User::where('name', 'Tom')->first(); $user->delete();
The above code will delete the user data named Tom
.
In short, Laravel provides a wealth of database operation methods, which can make us more convenient when developing web applications. When setting up the database, we need to pay attention to the setting of environment variables and the editing of database migration files to avoid unnecessary errors. At the same time, Eloquent ORM also provides us with convenient and fast CRUD operation methods, which can make us more efficient in the development process.
The above is the detailed content of laravel database settings. For more information, please follow other related articles on the PHP Chinese website!