Install Eloquent with composer

All pages in the this chapter assume that you have some experience with composer. Therefor it is not a manual to learn composer or even Laravel's Eloquent.

config.php, activate composer autoload

In folder application\config:

$config['composer_autoload'] = true;

Now install Laravel's eloquent:

Do the following in the application folder !!!

Make sure you have the following entries in the composer.json file:

{
  "license": "MIT",
  "require": {
    "illuminate/database": "^5.8",
    "illuminate/events": "^5.8"
  },
  ...
  "autoload": {
    "psr-4": {
      "App\\": "",
      "Db\\": "database/"
    }
  }
  ...
}

Now we we have namespaces like: Db\Eloquent\Models\Cars and Db\Eloquent\Models\Cars\Events

Install the dependencies

composer install

Without the Database connection, it will not work, so please read this below !!!

Database connection

We add the database connection information to the database.php file in the config folder of the application:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'db_user_name',
    'password' => 'db_password',
    'database' => 'cars_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'autoinit' => TRUE,
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array()
);

/**
 * Capsule from Laravel
 */
$capsule = new Capsule;
$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => $db['default']['hostname'],
    'database'  => $db['default']['database'],
    'username'  => $db['default']['username'],
    'password'  => $db['default']['password'],
    'charset'   => $db['default']['char_set'],
    'collation' => $db['default']['dbcollat'],
    'prefix'    => $db['default']['dbprefix'],
]);

// Event dispatcher, not required, but very handy
$capsule->setEventDispatcher(new Dispatcher(new Container())); 

$capsule->setAsGlobal();
$capsule->bootEloquent();

// Query logging, you can also commented it out, when you don't need it
$capsule->connection()->enableQueryLog();

A little bit more work to finish it

Now it should look like this:

|- application
|--- config
|--- controllers
|--- core
|--- database
|------ Eloquent
|---------- Models
|--- helpers
|--- hooks
|--- ...

Now head over to the next chapter, to learn how to use it.

Last update: March 14th at 1:24pm