Laravel is a PHP-based web development framework that can help developers build web applications more quickly and efficiently. MySQL is a popular relational database management system that is widely used in various web applications.
In Laravel, we can easily perform add, delete, modify and query operations on the MySQL database. Next, let us introduce in detail how to use Laravel to perform add, delete, modify and query MySQL.
1. Connect to the database
Laravel uses Eloquent ORM (Object Relational Mapping) for database operations by default, so we need to configure the database connection. In Laravel's configuration file, we only need to set the database address, username and password.
Open the config/database.php file, you can see the following configuration information:
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ],
Among them, we need to set the host, port, database, username and password information, respectively, for specifying Database address, port number, database name, username and password. In Laravel, we usually put this information in the .env file for configuration.
2. Create a model
In Laravel, a model represents a database table, which decouples the table from the application code. Therefore, we need to first create a model to operate our MySQL database.
In Laravel, creating a model is very simple using the artisan command line tool. Run the following command:
php artisan make:model User
This will create a User.php file in the app directory, which is the User model we created. We can write the following code in this file:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $table = 'users'; protected $fillable = ['name', 'email', 'password']; }
In the above code, we specify the name of the data table we want to operate as users. We can also define some attributes in the model to specify some default configuration information. For example, the $fillable attribute can specify which fields can be assigned values in batches, thus improving the security of the application.
3. Add, delete, modify and query
Inserting data is the process of adding a new piece of data to the database table. In Laravel, we can use the create method of the Eloquent model to save the data of a new model. Next we can look at an example:
$user = new User; $user->name = 'John Doe'; $user->email = 'johndoe@example.com'; $user->password = 'password'; $user->save();
Alternatively, we can also use the following method to create a new model and save it to the database:
User::create(['name' => 'John Doe', 'email' => 'johndoe@example.com', 'password' => 'password']);
To update data, we can use the save method on the model instance. We can retrieve the model instance from the database:
$user = User::find(1); $user->name = 'New Name'; $user->save();
Or we can update multiple pieces of data at once, as follows:
User::where('id', 1)->update(['name' => 'New Name']);
We can use the get method of the model instance to retrieve data in the database table, as shown below:
$users = User::all();
We can use the where method to perform conditional queries:
$users = User::where('name', 'John')->where('age', '>', 18)->get();
To delete data, we can use the delete method of the model instance:
$user = User::find(1); $user->delete();
Or we can delete multiple records at once:
User::where('votes', '<', 100)->delete();
Summary
The above are the related operations of using MySQL database to add, delete, modify and query in Laravel, including connecting to the database, creating models, inserting data, updating data, querying data and deleting data, etc. Laravel's design can help developers complete these operations more quickly, and also provides some convenient methods for querying, updating and other related operations. If you are developing a web application and need to use a MySQL database, Laravel will be a very good choice.
The above is the detailed content of How to use Laravel to add, delete, modify and query MySQL. For more information, please follow other related articles on the PHP Chinese website!