Skip to content

Instantly share code, notes, and snippets.

@craighooghiem
Forked from clauddiu/eloquent.php
Last active November 27, 2021 09:27
Show Gist options
  • Save craighooghiem/9979298 to your computer and use it in GitHub Desktop.
Save craighooghiem/9979298 to your computer and use it in GitHub Desktop.
Using Eloquent outside of Laravel. This specific example was used in a Silex installation.
<?php
require 'vendor/autoload.php';
$settings = array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'collation' => 'utf8_general_ci',
'prefix' => '',
'charset' => 'utf8'
);
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new Illuminate\Container\Container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
/** use Eloquent **/
use Illuminate\Database\Eloquent\Model as Model;
class Admin extends Model {
/** This property is required at the momment **/
/** I hope this to be fixed soon **/
public $table = "admin";
public $timestamps = false;
}
var_dump( Admin::all() );
@Arul-
Copy link

Arul- commented Jun 16, 2021

As an alternative use https://packagist.org/packages/laravel/database which provides eloquent with migration and more to non laravel projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment