Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Management

Advanced

Raw object

PDO object

Debug

Information

version: 2.1.12

Collaboration with other frameworks

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

Laravel

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

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

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

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

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

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

	return json_encode($data);
});

Slim

You can create a 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([
		'type' => 'mysql',
		'host' => 'localhost',
		'database' => 'name',
		'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();

Slim v4

The latest version of Slim v4 has changed the way to use containers manage application dependencies. You need to install PHP-DI additionally as a container for Slim's AppFactory, and access Medoo service from $this->get('service').

Make sure PHP-DI is installed
$ composer require php-di/php-di
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

// For using PHP-DI container.
use DI\Container;
// For using Medoo.
use Medoo\Medoo;

require __DIR__ . '/vendor/autoload.php';

// Create Container using PHP-DI.
$container = new Container();

// Set container to create App with on AppFactory.
AppFactory::setContainer($container);

$app = AppFactory::create();

// Set database as the name of Medoo service.
$container->set('database', function () {
	return new Medoo([
		'type' => 'mysql',
		'host' => 'localhost',
		'database' => 'name',
		'username' => 'your_username',
		'password' => 'your_password'
	]); 
});

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

	// Get access Medoo from $this->get('database').
	$data = $this->get('database')->select('account', ['id', 'name']);

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

$app->run();