Jens Segers on Jun 10 2014

Using Eloquent without Laravel

Not many people know this, but you can actually use the Laravel database component outside the Laravel framework (PyroCMS is actually using it in their CMS). This means you can use Eloquent in whatever project you want! The database component contains a Capsule manager class, which was originally created by Dan Horrigan, that takes care of creating a Laravel container and loading the required classes.

To use the database component, include it in your project with the following command, or by editing your composer.json file, and run composer install.

composer require illuminate/database

Then it's basically following the readme on how to use the Capsule manager.

require 'vendor/autoload.php';    

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',

// Make this Capsule instance available globally via static methods

// Setup the Eloquent ORM

That's it, now you can start using all of the awesome Eloquent and query builder stuff. For the query builder you can use the static Capsule methods:

$users = Capsule::table('users')->where('votes', '>', 100)->get();

And for Eloquent, just extend the Eloquent model like this:

class User extends Illuminate\Database\Eloquent\Model {}

$users = User::where('votes', '>', 1)->get();

For more information about the query builder and Eloquent, check out


Sumit Kumar 4 years ago

Is there a way to use read write connection without using entire laravel framework.

jenssegers 4 years ago

@Sumit check out Laravel's capsule manager.

volc 4 years ago

Thank you very much! This helped me a lot :)

Igor 4 years ago

How do i get query string from Capsule object?

marcosolare 4 years ago

nice - was looking for this exactly... One catch though: You might add that one should add

require 'vendor/autoload.php';

to the file in which you setup the connection (this was the case for me at least)

Arul Kumaran 3 years ago

I have created a package that uses Capsule as above to provide eloquent support for web apps and has more for the console application artisan to enable migration seeding etc. Take a look at (Laravel Database)[]

edgreenberg 3 years ago

This was perfect. Thank you.

vikasavnish 2 years ago

Your resource is very useful. I took out pain of writing sql again and again. I also bookmarked it and mentioned in my forum excursively.

mt 1 year ago

where to put the following statement ```$capsule->getDatabaseManager()->extend('mongodb', function($config) { return new Jenssegers\Mongodb\Connection($config); });

if i want to use capsule manager way of accessing jensseges/mongodb ?

olidev 1 year ago

When using Elqouent in PHP apps without Laravel, you have to create classes in your model in composer.json.

[email protected] 11 months ago

lace wigs,bob wigs accord agile, envoi très soigné, produit en compagnie de très adorable qualité. Indulgence!