Installing TDBM in Laravel

Laravel 5.x integration

Installation

To install TDBM in Laravel 5.x.

First, make sure your database is correctly configured in Laravel. Then run:

composer require thecodingmachine/tdbm-laravel ^5.0

For Laravel version 5.0 to 5.4, you will have to register the TheCodingMachine\TDBM\Laravel\Providers\TdbmServiceProvider manually in your application configuration file (config/app.php). For Laravel 5.5+, this is automatic, you don't need to do anything.

Generating beans and DAOs

When installation is done, you need to generate DAOs and beans from your data model.

Run the following command:

php artisan tdbm:generate
You must run this command after the installation of the package, and each time you run a migration (i.e. each time the database model changes).

Advanced configuration

By default, TDBM will write DAOs in the App\Daos namespace and beans in the App\Beans namespace. If you want to customize this, you can:

Create the config/tdbm.php file:

php artisan vendor:publish

Now you can edit the config/tdbm.php file:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | TDBM Configuration
    |--------------------------------------------------------------------------
    |
    | Use this configuration to customize the namespace of DAOs and beans.
    | These namespaces must be autoloadable from Composer.
    | TDBM will find the path of the files based on Composer.
    |
    */

    'daoNamespace' => 'App\\Daos',
    'beanNamespace' => 'App\\Beans',
];

Accessing DAOs

In Laravel, you would typically inject the DAOs in your services/controllers constructor.

Typically:

<?php
namespace App\Http\Controllers;

use App\Daos\MigrationDao;
use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * @var UserDao
     */
    private $userDao;

    /**
     * The DAO we need is injected in the constructor
     */
    public function __construct(UserDao $userDao)
    {
        $this->userDao = $userDao;
    }

    public function index($id)
    {
        $user = $this->userDao->getById($id);
        // do stuff
    }
}

Next step

Let's now learn how to access the database.

Found a typo? Something is wrong in this documentation? Just fork and edit it!