Feb 25, 2023
Laravel is one of the most popular PHP web frameworks in the world. It is known for its clean, elegant syntax, powerful features, and excellent documentation. Two of the most powerful tools in the Laravel toolbox are the Collection and Eloquent ORM.
In this article, we will explore how to use these tools with core PHP, outside of the Laravel framework.
The Laravel Collection is a powerful utility class that provides a convenient way to work with arrays and data sets. It provides a fluent, chainable interface for working with arrays, making it easy to filter, sort, and manipulate data.
To use the Laravel Collection in your PHP project, you can install the Illuminate/Support package via Composer. Here is an example of how to create a new collection and perform some basic operations on it:
use Illuminate\Support\Collection;
$items = collect([1, 2, 3, 4, 5]);
// Get the sum of the items
$sum = $items->sum();
// Get the average of the items
$average = $items->average();
// Filter the items to get only the even numbers
$evens = $items->filter(function ($item) {
return $item % 2 === 0;
});
As you can see, the Collection provides a clean and expressive syntax for working with arrays. You can also chain multiple operations together to create more complex queries:
$items = collect([1, 2, 3, 4, 5]);
$filtered = $items
->filter(function ($item) {
return $item % 2 === 0;
})
->map(function ($item) {
return $item * 2;
});
// Returns [4, 8]
Laravel Eloquent is a powerful ORM that provides a simple and elegant way to work with databases. It is built on top of the PDO library, and provides a fluent, expressive syntax for working with database records.
To use Eloquent in your PHP project, you can install the Illuminate/Database package via Composer. Here is an example of how to create a new model and perform some basic operations on it:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as Capsule;
// Configure the database connection
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'my_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
// Create a new model
class User extends Model {
protected $table = 'users';
}
// Find a user by ID
$user = User::find(1);
// Update the user's name
$user->name = 'John Doe';
$user->save();
// Delete the user
$user->delete();
As you can see, Eloquent provides a clean and expressive syntax for working with database records. You can also define relationships between models, perform complex queries, and use other advanced features like eager loading and query scopes.
The Laravel Collection and Eloquent ORM are two powerful tools that can help you write clean, expressive code in your PHP projects. While they are designed to be used with the Laravel framework, you can also use them in standalone PHP projects to make your code more expressive and efficient. By using these tools, you can write code that is easy to read, maintain, and extend.