Skip to content

Instantly share code, notes, and snippets.

@icaroscherma
Created March 28, 2019 18:10
Show Gist options
  • Save icaroscherma/e444dedd8b67ad1cf07843a268e13461 to your computer and use it in GitHub Desktop.
Save icaroscherma/e444dedd8b67ad1cf07843a268e13461 to your computer and use it in GitHub Desktop.
Using Eloquent Model without the whole Laravel structure
<?php
class EloquentModel extends Illuminate\Database\Eloquent\Model {
public function getConnection()
{
// In case of Phalcon 3.4
$di = new \Phalcon\Di();
$config = $di->getDefault()->getShared('config');
$db = $config['database'];
$host = explode(':', $db->host);
$host = 'mysql:host='.$host[0].';port='.$host[1].';dbname='.$db->dbname;
$username = $db->username;
$password = $db->password;
// If you are using structured PHP or another project, just remember to use this syntax:
// $host = 'mysql:host=172.17.0.1;port=3306;dbname=mydatabase';
return new Illuminate\Database\MySqlConnection(
new PDO($host, $username, $password)
);
}
}
/*
* Then create a class extending `EloquentModel`, remember to use the Classname
* as the singular of the table name, otherwise you will have to set it manually
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment