Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loilo/contao-illuminate-database-bundle
💾 Use Laravel's Illuminate Database abstraction in Contao
https://github.com/loilo/contao-illuminate-database-bundle
Last synced: about 23 hours ago
JSON representation
💾 Use Laravel's Illuminate Database abstraction in Contao
- Host: GitHub
- URL: https://github.com/loilo/contao-illuminate-database-bundle
- Owner: loilo
- License: mit
- Created: 2019-07-27T12:03:42.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-23T23:04:27.000Z (about 4 years ago)
- Last Synced: 2024-12-31T03:12:37.953Z (about 2 months ago)
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Illuminate Database for Contao
[](https://github.com/loilo/contao-illuminate-database-bundle/actions)
[](https://packagist.org/packages/loilo/contao-illuminate-database-bundle)Use Laravel's [Illuminate Database](https://laravel.com/docs/queries) abstraction in Contao with support for Contao models.
## Installation
```bash
composer require loilo/contao-illuminate-database-bundle
```## Usage
### Getting Started
Get the `db()` helper function:```php
use function Loilo\ContaoIlluminateDatabaseBundle\Database\db;
```Calling the `db()` function creates a new [Laravel query builder](https://laravel.com/docs/queries) instance.
### Basic Queries
This is how we'd fetch ID and name of the earliest admin of the Contao installation:```php
$row = db()
->select('id', 'name')
->from('user')
->where('admin', '1')
->orderBy('dateAdded')
->first();
```> Note how the `tl_` prefix is automatically prepended to table names, so we actually read from `tl_user`.
The above is just a very basic example. To get an idea of what's possible with this API, consult the [Laravel docs](https://laravel.com/docs/queries).
### Fetching Models
In addition to Laravel's built-in methods, the query builder of this package exposes an additional `asModel()` method.Using it inside a query builder chain will instruct the `get()`, `first()`, `find()` and `cursor()` methods to return Contao models instead of plain database records.
To explain this based on the example above:
```php
$user = db()
->from('user')
->asModel() // <- notice this line
->where('admin', '1')
->orderBy('dateAdded')
->first();// $user will be an instance of \UserModel
```### Customizing Connections
The `db()` function takes an optional argument which may override keys from the default connection configuration [passed to Laravel's connection manager](https://laravel.com/api/5.8/Illuminate/Database/Capsule/Manager.html#method_addConnection):```php
// Set an empty prefix to use the "tl_user" table
db([ 'prefix' => '' ])->from('tl_user')->first();
```