It's pretty straightforward to use Eloquent outside of the Laravel Framework. Let's look at all the steps to get it working, whether you want to pull it into another framework or use it in procedural code.
First up, install the illuminate/database
package.
composer require illuminate/database
Next, do the following somewhere in your non-Laravel application where you're bootstrapping. Bear in mind this is a really simplified, procedural example with hard-coded values, which you'll ideally want to pull from a .env
file.
use Illuminate\Database\Capsule\Manager;
$config = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'some_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
];
$capsule = new Manager();
$capsule->addConnection($config, 'mysql');
$capsule->bootEloquent();
$capsule->getDatabaseManager()->setDefaultConnection('mysql');
Let's run over what's happening here.
Manager
is the class used to handle everything, so we new it up, and use addConnection
to pass in the config and select the driver.bootEloquent
so we can globally use Models.Once you've bootstrapped Eloquent, creating models is a breeze. In fact, it's precisely what you'd do in a standard Laravel application.
Here's an example User
model.
use Illuminate\Database\Eloquent\Model;
class User extends Eloquent
{
protected $fillable = [
'name',
'email',
'password'
];
}
You should now be able to use the User
model directly to read, update, create, and do anything else you need.
$users = User::get(); // returns a Collection
I recently implemented Eloquent in the Build Your Own PHP Framework course. Let's take a look at what the service provider looks like (using League Container).
class DatabaseServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
{
protected Manager $capsule;
public function boot(): void
{
$config = $this->getContainer()->get(Config::class);
$capsule = new Manager();
$driver = $config->get('database.driver');
$capsule->addConnection(
$config->get('database.' . $driver),
$driver
);
$capsule->bootEloquent();
$capsule->getDatabaseManager()->setDefaultConnection($driver);
$this->capsule = $capsule;
}
public function register(): void
{
$this->getContainer()->add(DatabaseManager::class, function () {
return $this->capsule->getDatabaseManager();
})
->setShared();
}
public function provides(string $id): bool
{
$services = [
DatabaseManager::class
];
return in_array($id, $services);
}
}
A lot is happening here, but it's an ideal solution for accessing the DatabaseManager
directly on the container (using the Fluent Query Builder instead of models) and booting Eloquent so we can use models, too.
It also pulls config from a separate config file that uses an .env
file, meaning we can easily swap between values in different environments. Here's what the config file looks like.
return [
'driver' => env('DB_DRIVER'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
],
];
Either way you implement Eloquent outside of Laravel, the first section of this article is all you really need to do — just structure it in a way that makes sense for your project, and you can start using Eloquent!