31

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sjosen
  • 551
  • 1
  • 6
  • 10
  • 2
    I'm using Eloquent standalone for a year and I can say: that's a terrible idea, please go with a real ORM like Doctrine and avoid massive suffering. – Machado Jul 31 '18 at 02:03
  • The Medoo project is still active. You can check out the project on GitHub https://github.com/catfan/Medoo. – Angolao Aug 01 '18 at 18:40

2 Answers2

55

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.

Bastian Hofmann
  • 2,485
  • 19
  • 19
  • thanks for the detailed reply.. i wanted to know if we could create an alias to Capsule::schema() set to Schema, so it can be used the same way as in Laravel ie Schema::Model->action() or Schema::table()->action() ? – rolfk Apr 08 '15 at 09:24
  • 1
    You could create a `Schema` class and redirect all static method calls to `Capsule::schema()->$method` with `__callStatic`. – Bastian Hofmann Jul 21 '15 at 08:31
  • How do you run migrations, or is that not possible? – Goddard Nov 27 '19 at 14:50
3

Bastian's answer is pretty well. But there is one more thing. It can't work without Event Lib. To install it;

composer require illuminate/events

You are good to go

merkdev
  • 931
  • 10
  • 8