PHP code example of jguyomard / silex-capsule-eloquent
1. Go to this page and download the library: Download jguyomard/silex-capsule-eloquent 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/ */
jguyomard / silex-capsule-eloquent example snippets
$app = new Silex\Application();
$app->register(
new \JG\Silex\Provider\CapsuleServiceProvider(),
[
'capsule.connections' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'root',
'password' => 'root',
]
]
]
);
$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();
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();
$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,
],
]
);