PHP code example of corephp / slim-eloquent

1. Go to this page and download the library: Download corephp/slim-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/ */

    

corephp / slim-eloquent example snippets


$options = [
    'driver'    => 'mysql|pgsql|sqlite',
    'host'      => 'host',
    'port'      => 3306,
    'database'  => 'database',
    'username'  => 'user',
    'password'  => 'pass',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
];

$options = [
    'driver' => 'sqlite',
    'database' => '/path/to.database',
    'foreign_key_constraints' => true,
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
];

use CorePHP\Slim\Dependency\Database\Parser;

$settings = [
    // ...
    "database" => Parser::parseConnection(
        getenv('DATABASE_URL')
    )
    // ...
];

use CorePHP\Slim\Dependency\Database\Eloquent;

return function ($container) {
    $settings = $container->get('eloquent');
    
    $eloquent = new Eloquent();
    $eloquent->addConnection($settings);

    return $eloquent->getManager();
};

use CorePHP\Slim\Dependency\Database\Eloquent;

return function ($container) {
    $eloquent = new Eloquent();
    $eloquent->addConnection($container['database'], 'default', 'r'); // read
    $eloquent->addConnection($container['database2'], 'default', 'w'); // write

    return $eloquent->getManager();
}

use CorePHP\Slim\Dependency\Database\Model;

class User extends Model
{
    protected $table = 'users';
}

$items = User::where('name', 'like', '%Jhon%')
    ->pagination(10, 1, true);

// The content of items variable will be:
//
// [
//    "data" => [ ... ]
//    "pages" => [
//      "limit" => 10,
//      "pages" => 100    /* depending of the total elemtns */
//      "total" => 1000   /* total elemtns of the query */
//      "page" => 1,
//      "links" => [ 1, 2, 3, 4, 5, 6, '...', 100 ]
//    ]
// ]