Home  >  Article  >  Backend Development  >  ORM extension library in PHP8.0: Eloquent

ORM extension library in PHP8.0: Eloquent

WBOY
WBOYOriginal
2023-05-14 10:22:351563browse

As developers’ needs for data interaction continue to grow, ORM has become an indispensable part of modern development. It can hide database operations in the background and provide a simplified API for CRUD operations. Among these ORM libraries, Eloquent has attracted the attention of many developers because it has been widely used in the Laravel framework. In PHP 8.0, Eloquent comes as a standalone extension library and can now be used in your projects.

In this article, we will explore some of Eloquent’s key features and introduce how to integrate this extension library in your PHP project.

Installing Eloquent

Before installing Eloquent, you need to install PHP8.0 or higher to ensure compatibility. Eloquent can be installed using [composer](https://getcomposer.org/), just run the following command in the terminal:

composer require illuminate/database

Eloquent Model

The basis for querying and modifying the database in Eloquent Is the model (Model). Models are the core concept of ORM and are usually mapped to database tables. Eloquent models provide a very convenient way to perform standard operations such as create, update, and delete.

In Eloquent, the Model class has definable properties and methods for interacting with database tables. Let’s look at a simple example:

use IlluminateDatabaseEloquentModel;

class User extends Model
{
    protected $fillable = ['name', 'email', 'password'];
}

This example defines a User model that contains fill attributes $fillable. This tells Eloquent which properties can be set during batch allocation. In this case, only the properties listed in the $fillable property can be set via array assignment.

Database connection

You can configure the database connection in the following ways:

use IlluminateDatabaseCapsuleManager as DB;

$db = new DB;

$db->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'username',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

$db->setAsGlobal();

Through the addConnection method, Eloquent will establish a database connection in the application . You can add multiple connections to each channel and use different connections in different scenarios. The

setAsGlobal method will make the database connection instantiated by this connection become a global connection, and this connection will be given priority in subsequent operations.

Create new records

You can use Eloquent to create new data records, for example:

$user = new User;
$user->name = 'John Doe';
$user->email = 'john.doe@example.com';
$user->password = 'secret';
$user->save();

Query records

You can query data through Eloquent, for example:

$users = User::all();

$user = User::find(1);

$users = User::where('active', true)
             ->orderBy('name', 'desc')
             ->take(10)
             ->get();

Update records

You can use Eloquent to update existing records, for example:

$user = User::find(1);
$user->name = 'New Name';
$user->save();

When changing records, you should use the save method. If you only want to update some specific fields, you can use the update method:

User::where('id', 1)
    ->update(['name' => 'New Name']);

Delete records

You can use Eloquent to delete existing records, for example:

$user = User::find(1);
$user->delete();

Conclusion

Eloquent is a powerful ORM library and is famous as a part of the Laravel framework. However, it can benefit even more PHP applications through a separate extension library. With this overview of this article, you should now have a better understanding of Eloquent and can start using it in your own PHP 8.0 applications.

The above is the detailed content of ORM extension library in PHP8.0: Eloquent. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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