Package Data | |
---|---|
Maintainer Username: | jguyomard |
Package Create Date: | 2016-04-16 |
Package Last Update: | 2016-12-03 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-11 15:26:27 |
Package Statistics | |
---|---|
Total Downloads: | 18,857 |
Monthly Downloads: | 59 |
Daily Downloads: | 3 |
Total Stars: | 6 |
Total Watchers: | 4 |
Total Forks: | 3 |
Total Open Issues: | 0 |
This is a Service Provider for Silex 2.0 that integrates Laravel's Fluent Query Builder and Eloquent ORM via Capsule.
Note: This Service Provider requires silex/silex ~2.0
.
composer require jguyomard/silex-capsule-eloquent "~2.0"
This is a basic configuration with MySQL (Currently, Laravel supports MySQL, Postgres, SQLite and SQL Server):
$app = new Silex\Application();
$app->register(
new \JG\Silex\Provider\CapsuleServiceProvider(),
[
'capsule.connections' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'root',
'password' => 'root',
]
]
]
);
This is a basic usage, using Query Builder or Raw SQL Queries:
$app->get('/article/{id}', function(Application $app, $id)
{
$article = Capsule::table('article')->where('id', $id)->get();
// Rest of your code...
});
$app->get('/raw/{id}', function(Application $app, $id)
{
$article = Capsule::select('SELECT * FROM article WHERE id = :id', [
'id' => $id,
]);
// Rest of your code...
});
$app->run();
You can also use Eloquent Models:
class ArticleModel extends Model
{
protected $table = 'article';
protected $primaryKey = 'id';
protected $fillable = [
'title'
];
// Rest of your code...
}
$app->get('/article/{id}', function(Application $app, $id)
{
$article = ArticleModel::find($id);
// Rest of your code...
});
$app->post('/article', function(Application $app)
{
$article = ArticleModel::create([
'title' => 'Foo'
]);
// Rest of your code...
});
$app->run();
For further documentation on using the various database facilities this library provides, consult the Laravel framework database documentation.
This is a complete configuration example, with multiple connections:
$app = new Silex\Application();
$app->register(
new \JG\Silex\Provider\CapsuleServiceProvider(),
[
'capsule.connections' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'mydatabase',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => 'localhost',
'port' => 5432,
'database' => 'mydatabase',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlite' => [
'driver' => 'sqlite',
'database' => 'mydatabase',
'prefix' => '',
],
],
'capsule.options' => [
'setAsGlobal' => true,
'bootEloquent' => true,
'enableQueryLog' => true,
],
]
);
To run the test suite, you need PHPUnit:
phpunit
Inspired by illuminate-database-silex-service-provider (for Silex 1.*) and saxulum-doctrine-mongodb-odm-provider@dev (Mongodb ODM for Silex 2.0.x-dev).
If you have any problems with or questions about this Service Provider, please contact me through a GitHub issue. If the issue is related to Capsule itself please leave an issue on Laravel official repository.
You are invited to contribute new features, fixes or updates to this container, through a Github Pull Request.