northern-lights / eloquent-bootstrap
Bootstrap for standalone Eloquent ORM
Requires
- php: ^7.3|^8.0
- illuminate/database: 8.50.0
- illuminate/pagination: 8.50.0
Requires (Dev)
- dev-master
- 8.50.0
- 8.49.2
- 8.49.1
- 8.49.0
- 8.48.2
- 8.48.1
- 8.48.0
- 8.47.0
- 8.46.0
- 8.45.1
- 8.45.0
- 8.44.0
- 8.43.0
- 8.42.1
- 8.42.0
- 8.41.0
- 8.40.0
- 8.39.0
- 8.38.0
- 8.37.0
- 8.36.2
- 8.36.1
- 8.36.0
- 8.35.1
- 8.35.0
- 8.34.0
- 8.33.1
- 8.33.0
- 8.32.1
- 8.32.0
- 8.31.0
- 8.30.1
- 8.30.0
- 8.29.0
- 3.0.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.0
- dev-dependabot/add-v2-config-file
- dev-develop
This package is auto-updated.
Last update: 2024-04-15 01:11:03 UTC
README
The Eloquent ORM that comes with Laravel makes it incredibly easy to interact with a database.
Unfortunately, if you want to use it standalone, without rest of framework, things are not so easy.
This library solves that headache for you, and brings Eloquent ORM to your project with single command.
Install
Via Composer
$ composer require northern-lights/eloquent-bootstrap
It really is that easy!
Usage - Single connection
<?php declare(strict_types=1); namespace NorthernLights\EloquentBootstrap\Example; use NorthernLights\EloquentBootstrap\Database; use NorthernLights\EloquentBootstrap\Provider\ConfigOptions; use NorthernLights\EloquentBootstrap\Provider\ConfigProvider; require __DIR__ . '/vendor/autoload.php'; $database = new Database(new ConfigProvider([ ConfigOptions::HOST => 'localhost', ConfigOptions::DATABASE => 'database_name', ConfigOptions::USERNAME => 'user', ConfigOptions::PASSWORD => 'pass' ])); // At this point, eloquent will boot $database->init();
Usage - Multiple connections
<?php declare(strict_types=1); namespace NorthernLights\EloquentBootstrap\Example; use NorthernLights\EloquentBootstrap\Connection; use NorthernLights\EloquentBootstrap\Database; use NorthernLights\EloquentBootstrap\Provider\ConfigOptions; use NorthernLights\EloquentBootstrap\Provider\ConfigProvider; require __DIR__ . '/vendor/autoload.php'; $database = new Database(); $database->addConnection( new Connection('first-database', new ConfigProvider([ ConfigOptions::HOST => 'localhost', ConfigOptions::DATABASE => 'first_database', ConfigOptions::USERNAME => 'user', ConfigOptions::PASSWORD => 'pass' ])) ); $database->addConnection( new Connection('second-database', new ConfigProvider([ ConfigOptions::HOST => 'localhost', ConfigOptions::DATABASE => 'second_database', ConfigOptions::USERNAME => 'second_user', ConfigOptions::PASSWORD => 'pass' ])) ); // At this point, eloquent will boot $database->init();
And that's all you need to include in your bootstrap file. For everything else, consult with Eloquent documentation.
Note: Even in this example, you can setup default connection via Database constructor.
Note: NorthernLights\EloquentBootstrap\Database::getCapsule() will return Capsule instance, which can be used to add connections directly
RAW Query usage
<?php (...) // Notice that you need this line too $database->getCapsule()->setAsGlobal(); $user = DB::table('users')->where('userid', '=', '123'); dump($user);
Creating a simple model
<?php declare(strict_types=1); namespace NorthernLights\EloquentBootstrap\Example; use NorthernLights\EloquentBootstrap\Model as EloquentModel; /** * Class Users * @package NorthernLights\EloquentBootstrap\Example */ class Users extends EloquentModel { /** @var string */ protected $table = 'users'; }
Note the usage of NorthernLights\EloquentBootstrap\Model, since it will only fix IDE annotations (Confirmed: PhpStorm). It doesn't include any logic.
PSR-12 Standard
Library strives to comply with PSR-12 coding standards, therefore we included following commands:
$ composer check-style $ composer fix-style
Note: Second command will actually modify files
PSR-4 Standard
Library complies with PSR-4 autoloading standard
Testing
$ composer php-lint
$ composer test
License
The MIT License (MIT). Please see License File for more information.