Home > PHP Framework > Laravel > body text

laravel database settings

王林
Release: 2023-05-29 09:16:07
Original
646 people have browsed it

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.

1. Environment variables

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=
Copy after login
  • 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.

2. Database migration

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:

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');
    }
}
Copy after login

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.

3. Model

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:

Copy after login

The above code will specify the use of mysql connection to access usersTable, 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.

3.1 Create data

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'),
]);
Copy after login

The above code will create a user named Tom, email address is tom@example.com, and password is password data.

3.2 Query data

In Eloquent ORM, we can use the get method to query data. For example:

$users = User::get();
Copy after login

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();
Copy after login

The above code will query all user data named Tom from the users table.

3.3 Update data

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();
Copy after login

The above code will change the email address of the user data named Tom to new_email@example.com.

3.4 Delete data

In Eloquent ORM, we can use the delete method to delete data. For example:

$user = User::where('name', 'Tom')->first();
$user->delete();
Copy after login

The above code will delete the user data named Tom.

Conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!