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.
In folder application\config:
$config['composer_autoload'] = true;
Now install Laravel's eloquent:
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/"
}
}
...
}
App
namespace root is pointing to the application
folder itself.Db
namespace root is pointing to the database
folder in the application
folder.Now we we have namespaces like: Db\Eloquent\Models\Cars and Db\Eloquent\Models\Cars\Events
composer install
Without the Database connection, it will not work, so please read this below !!!
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();
database
.database
folder a folder named Eloquent
(mind the first capital!)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.