Stand Alone Usage
This documentation demonstrates how to use the package outside of Laravel.
- add
"yajra/laravel-oci8": "6.0.*"
on your composer then runcomposer install
- create
database.php
and add the code below
require 'vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule;use Yajra\Oci8\Connectors\OracleConnector;use Yajra\Oci8\Oci8Connection; $capsule = new Capsule; $manager = $capsule->getDatabaseManager();$manager->extend('oracle', function($config){ $connector = new OracleConnector(); $connection = $connector->connect($config); $db = new Oci8Connection($connection, $config["database"], $config["prefix"]); // set oracle session variables $sessionVars = [ 'NLS_TIME_FORMAT' => 'HH24:MI:SS', 'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', 'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', 'NLS_TIMESTAMP_TZ_FORMAT' => 'YYYY-MM-DD HH24:MI:SS TZH:TZM', 'NLS_NUMERIC_CHARACTERS' => '.,', ]; // Like Postgres, Oracle allows the concept of "schema" if (isset($config['schema'])) { $sessionVars['CURRENT_SCHEMA'] = $config['schema']; } $db->setSessionVars($sessionVars); return $db;}); $capsule->addConnection(array( 'driver' => 'oracle', 'host' => 'oracle.host', 'database' => 'xe', 'username' => 'user', 'password' => 'password', 'prefix' => '', 'port' => 1521)); $capsule->bootEloquent();
Set the event dispatcher used by Eloquent models (Optional).
use Illuminate\Events\Dispatcher;use Illuminate\Container\Container;$capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional)$capsule->setAsGlobal();
Now we can start working with database tables just like we would if we were using Laravel!
require 'database.php'; // Create the User modelclass User extends Illuminate\Database\Eloquent\Model { public $timestamps = false;} // Grab a user with an id of 1$user = User::find(1); echo $user->toJson(); die();