sjosen
sjosen

Reputation: 561

Can I use Laravel's database layer standalone?

For some time now I have been looking towards using a php framework for my work. I've been writing procedural style until recently and is still trying to find my way around the oop world/style. I figured that a php framework would help me write better code and I'm pretty sure I'll lean towards the Laravel project in a near future.

Right now I'm in need of a database layer that I can use in my existing code. I use mysqli with prepared statements right now, as it was easy for me to implement (using MySQL before).

I've been looking at http://medoo.in as an "easy" way to use a pdo wrapper/class but the lack of activity on the support page, and the fact that I'm working on using Laravel in the future, made me wonder if I could use the Laravel database layer now for my existing code.

Could this be done and would it make sense or am I misunderstanding and mixing concepts of code styling?

Upvotes: 32

Views: 17253

Answers (1)

Bastian Hofmann
Bastian Hofmann

Reputation: 2495

IMO it's absolutely valid to transition to an OOP approach step by step.

To your question:

Yes, you can use Eloquent standalone.

Here is the packagist site: https://packagist.org/packages/illuminate/database Add "illuminate/database": "5.0.*@dev" to your composer.json and run composer update. Now you'll need to bootstrap Eloquent. (https://github.com/illuminate/database)

The following is copied from the repo's readme:

Usage Instructions

First, create a new "Capsule" manager instance. Capsule aims to make configuring the library for usage outside of the Laravel framework as easy as possible.

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'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

Once the Capsule instance has been registered. You may use it like so:

Using The Query Builder

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

Other core methods may be accessed directly from the Capsule in the same manner as from the DB facade:

$results = Capsule::select('select * from users where id = ?', array(1));

Using The Schema Builder

Capsule::schema()->create('users', function($table)
{
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

Using The Eloquent ORM

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

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

For further documentation on using the various database facilities this library provides, consult the Laravel framework documentation.

Upvotes: 55

Related Questions