Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Laravel's database layer standalone?

Tags:

php

mysql

laravel

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?

like image 820
sjosen Avatar asked Sep 28 '14 08:09

sjosen


People also ask

Can you use eloquent without Laravel?

If you want, you can use Eloquent without Laravel. Actually, Laravel is not a monolithic framework. It is made up of several, separate parts, which are combined together to build something greater. However, nothing prevents you from using only selected packages in another application.

What are available database supported by Laravel?

Currently Laravel supports four database systems: MySQL, Postgres, SQLite, and SQL Server.


1 Answers

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.

like image 191
Bastian Hofmann Avatar answered Sep 17 '22 17:09

Bastian Hofmann