Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Advanced

Raw object

PDO object

Debug

Information

version: 1.6.1

Collaboration with other frameworks

Medoo is working friendly with other frameworks together. The singleton design pattern that most of full-stack frameworks supported, is the better way for Medoo to collaborate with those, which is registered the database instance as reference only once and can be reusable without connecting the database again. We recommend this if you are using framework that supported. For more details, you can read about their official documentation about how to co-work with third-party library.

Laravel

Laravel provided singleton function to register a new singleton object. You can create and return the Medoo object there. The database configuration can be set on config file and load back via Config::get().

Registration on app.php
// Use Medoo namespace
use Medoo\Medoo;

// Register as database
$app->singleton('database', function () {
	return new Medoo([
		'database_type' => 'mysql',
		'database_name' => 'name',
		'server' => 'localhost',
		'username' => 'your_username',
		'password' => 'your_password'
	]);
});
Accessing Medoo

After registered via singleton, you can access the Medoo object via using $this->app->database, and started using any Medoo APIs.

Route::get('/', function () {

	$data = $this->app->database->select('account', ['id', 'name']);

	return json_encode($data);
});

Slim

You can create Medoo object to the app's container array, and you can access it via $this->database.

require './vendor/autoload.php';

use Medoo\Medoo;

$app = new \Slim\App();

$container = $app->getContainer();

$container['database'] = function () {
	return new Medoo([
		'database_type' => 'mysql',
		'database_name' => 'name',
		'server' => 'localhost',
		'username' => 'your_username',
		'password' => 'your_password'
	]); 
};

$app->get('/', function($request, $response, $args) {

	$data = $this->database->select('account', ['id', 'name']);

	return $response->write(json_encode($data));
});

$app->run();