PHP code example of singular / capsule
1. Go to this page and download the library: Download singular/capsule library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
singular / capsule example snippets
$app = new Silex\Application;
$app->register(new Electrolinux\Silex\Provider\CapsuleServiceProvider, [
'capsule.connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true,
],
]);
$app['capsule'];
use Illuminate\Database\Capsule\Manager as Capsule;
$app->get('/book/{id}', function(Application $app, $id) {
$book = Capsule::table('books')->where('id', $id)->get();
return $app->json($book);
});
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $table = 'books';
protected $fillable = [
'title',
'author',
];
protected $casts = [
'title' => 'string',
'author' => 'string',
];
// The rest of your model code...
}
$app->get('/books', function(Application $app) {
$books = Book::with('tags')->all();
return $app->json($books);
});
$app->post('/book', function(Application $app, Request $request) {
$book = new Book();
$book->title = $request->request->get('title');
$book->author = $request->request->get('author');
$book->save();
});
$app = new Silex\Application;
$app->register(new Electrolinux\Silex\Provider\CapsuleServiceProvider, [
// Connections
'capsule.connections' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dname1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => false,
],
'other' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true,
],
],
]);
Capsule::connection($name)->getQueryLog();
$app['capsule']->schema()->create('books', function($table) {
$table->increments('id');
$table->string('title');
$table->string('author');
$table->timestamps();
});
class BookObserver
{
public function saving($model)
{
// Do something
}
}
Book:observe(new BookObserver());
$app = new Silex\Application;
$app->register(new Electrolinux\Silex\Provider\CapsuleServiceProvider, [
// Multiplas conexões
'capsule.connections' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dname1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => false, // Desabilita o log para esta conexão
],
'other' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Habilita o log para esta conexão
],
],
/*
// Conexão única
'capsule.connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'logging' => true, // Habilita o log para esta conexão
],
*/
/*
// Outras opções
'capsule.global' => true, // Habilita o acesso global ao query builder do Capsule.
'capsule.eloquent' => true, // Automaticamente inicializa o Eloquent ORM.
*/
]);