Skip to content

Instantly share code, notes, and snippets.

@Tharwat96
Created June 26, 2020 21:25
Show Gist options
  • Save Tharwat96/28879de458e04103617ff2a0993b0713 to your computer and use it in GitHub Desktop.
Save Tharwat96/28879de458e04103617ff2a0993b0713 to your computer and use it in GitHub Desktop.
Eloquent ORM Without Laravel

Eloquent ORM Without Laravel

  1. Use composer to install illuminate/database

composer require illuminate/database

  1. Place database.php and its contents wherever you would like (preferebely at config/database.php or bootstrap.php)

note the path used since we will use that path at our files

  1. Create any php script and start using it like any.php file while noting the path to be valid to you
// any file to use the Eloquent driver
<?php
require "../vendor/autoload.php";
require "../config/database.php";
use Illuminate\Database\Capsule\Manager as Capsule;
$dbclass = new Database();
$capsule = new Capsule();
$capsule->addConnection($dbclass->EloquentConnection());
// Make this Capsule instance available globally via static methods
$capsule->setAsGlobal();
// Setup the Eloquent ORM
$capsule->bootEloquent();
$users = Capsule::table('user')->where('id', '=', 1)->get();
// config/database.php
// database connection file
<?php
class Database
{
// specify your own database credentials
private $host = '127.0.0.1';
private $db_name = 'eloquent';
private $username = 'root';
private $password = 'password';
public $conn;
// public function __construct() {
// }
public function EloquentConnection()
{
return [
'driver' => 'mysql',
'host' => $this->host,
'database' => $this->db_name,
'username' => $this->username,
'password' => $this->password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment