Created
March 28, 2019 18:10
-
-
Save icaroscherma/e444dedd8b67ad1cf07843a268e13461 to your computer and use it in GitHub Desktop.
Using Eloquent Model without the whole Laravel structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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