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;

$capsule->addConnection([
    '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
$capsule->setAsGlobal();

// Setup the Eloquent ORM
$capsule->bootEloquent();

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 http://laravel.com/docs/eloquent


Comments

[email protected] 3 months ago

lace wigs,bob wigs https://youtu.be/3KO3e4I3_24 accord agile, envoi très soigné, produit en compagnie de très adorable qualité. Indulgence!


olidev 1 year ago

When using Elqouent in PHP apps without Laravel, you have to create classes in your model in composer.json. https://www.cloudways.com/blog/eloquent-illuminate-in-php-without-laravel/


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 ?

vikasavnish 1 year 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.

https://www.servloci.com/t/php-web-development-resources/50/1


edgreenberg 2 years ago

This was perfect. Thank you.


Arul Kumaran 2 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)[https://github.com/Luracast/Laravel-Database]


marcosolare 3 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) https://github.com/illuminate/database/blob/master/README.md


Igor 3 years ago

How do i get query string from Capsule object?


volc 3 years ago

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


jenssegers 4 years ago

@Sumit check out Laravel's capsule manager.


Sumit Kumar 4 years ago

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