loilo / contao-illuminate-database-bundle
Use Laravel's Illuminate Database abstraction in Contao
Installs: 65
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:contao-bundle
Requires
- php: >= 7.1
- illuminate/database: ^5.8
Requires (Dev)
- mockery/mockery: ^1.2
- phpunit/phpunit: ^7.5
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-11-24 07:57:10 UTC
README
Use Laravel's Illuminate Database abstraction in Contao with support for Contao models.
Installation
composer require loilo/contao-illuminate-database-bundle
Usage
Getting Started
Get the db()
helper function:
use function Loilo\ContaoIlluminateDatabaseBundle\Database\db;
Calling the db()
function creates a new Laravel query builder instance.
Basic Queries
This is how we'd fetch ID and name of the earliest admin of the Contao installation:
$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 fromtl_user
.
The above is just a very basic example. To get an idea of what's possible with this API, consult the Laravel docs.
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:
$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:
// Set an empty prefix to use the "tl_user" table db([ 'prefix' => '' ])->from('tl_user')->first();