Laravel-OCI8

repository·master·Indexed 21 days ago

https://github.com/yajra/laravel-oci8

An Oracle Database Driver for Laravel that extends Illuminate/Database to communicate with Oracle databases using the PHP OCI8 extension. It provides support for Oracle-specific features including PL/SQL functions and procedures, Ref Cursors, sequence management, BLOB handling via OracleEloquent, and version-specific capabilities for Oracle 11g, 12c, 19c, and 21c.

Tokens
7.9K
Snippets
28
Records
48
Agent score
75%

What's inside yajra/laravel-oci8

  1. Configure Oracle Server Version

    master

    The package behavior changes based on the Oracle version. Set the version using the DB_SERVER_VERSION environment variable or the server_version key in config/oracle.php.

    • 11g: The baseline version (default).
    • 12c: Enables fetch/offset instead of rownumber, identity columns for schema builder, and JoinLateral support.
    • 12cR2: Enables binary ci for case-insensitive whereLike queries.
    • 19c: Enables JSON path updates via query builder.
    • 21c: Uses native JSON type instead of CLOB in schema builder.
  2. Use the Oracle User Provider for Authentication

    master

    Oracle queries are case-sensitive by default, which can cause authentication issues. To handle this, use the oracle driver for your user provider in config/auth.php.

    'providers' => [
        'users' => [
            'driver' => 'oracle',
            'model' => App\User::class,
        ],
    ],
  3. Register the Laravel-OCI8 Service Provider

    master

    For Laravel versions older than 5.5, you must manually register the service provider. Open config/app.php and add Yajra\Oci8\Oci8ServiceProvider::class to the providers array.

    Yajra\Oci8\Oci8ServiceProvider::class,
  4. Configure Oracle connection for Laravel Lumen

    master

    Lumen does not automatically load all configuration files. Ensure you have a config/database.php file and append the oracle connection array to it. You can then use .env variables to populate these values.

    'oracle' => [
        'driver'         => 'oracle',
        'tns'            => env('DB_TNS', ''),
        'host'           => env('DB_HOST', ''),
        'port'           => env('DB_PORT', '1521'),
        'database'       => env('DB_DATABASE', ''),
        'service_name'   => env('DB_SERVICE_NAME', ''),
        'username'       => env('DB_USERNAME', ''),
        'password'       => env('DB_PASSWORD', ''),
        'charset'        => env('DB_CHARSET', 'AL32UTF8'),
        'prefix'         => env('DB_PREFIX', ''),
        'prefix_schema'  => env('DB_SCHEMA_PREFIX', ''),
        'edition'        => env('DB_EDITION', 'ora$base'),
        'server_version' => env('DB_SERVER_VERSION', '11g'),
        'load_balance'   => env('DB_LOAD_BALANCE', 'yes'),
        'connect_timeout' => env('DB_CONNECT_TIMEOUT', ''),
        'retry_count'    => env('DB_RETRY_COUNT', '3'), // 12c and above only
        'retry_delay'    => env('DB_RETRY_DELAY', '1'), // 12c and above only
        'transport_connect_timeout' => env('DB_TRANSPORT_CONNECT_TIMEOUT', '60'), // 12c and above only
        'expire_time'    => env('DB_EXPIRE_TIME', '0'), // 19c and above only
        'dynamic'        => [],
        'max_name_len'   => env('ORA_MAX_NAME_LEN', 30),
    ],
  5. Configure Laravel-OCI8 via Artisan

    master

    To customize the package settings, publish the configuration file to config/oracle.php. If you do not publish the file, the package will automatically use the database configuration defined in your .env file.

    php artisan vendor:publish --tag=oracle
  6. Query JSON data in Oracle

    master

    The OracleGrammar provides specialized support for querying JSON columns using Oracle's JSON capabilities. This allows you to use Laravel's JSON query builder methods which are then translated into Oracle-specific SQL like JSON_TABLE or JSON_EXISTS.

    Key supported operations include:

    • JSON Contains: Checking if a JSON column contains a specific value or key using JSON_TABLE and EXISTS.
    • JSON Key Existence: Using json_exists to check for the presence of a path.
    • JSON Length: Calculating the number of elements in a JSON array using JSON_TABLE and COUNT(*).
  7. Manage Oracle database schema with OracleBuilder

    master

    The OracleBuilder class extends Laravel's base Schemauilder to provide Oracle-specific schema management capabilities. It handles specialized Oracle operations such as auto-increment object management, DDL preferences (like ctxsys for full-text search), and schema/table parsing.

    Key capabilities include:

    • Table Lifecycle: Creating, modifying, and dropping tables while automatically managing associated auto-increment objects and DDL preferences.
    • Bulk Operations: Dropping all tables, views, or types in the database.
    • Introspection: Retrieving column listings, column details, indexes, and table lists.
    • Constraint Management: Disabling and enabling foreign key constraints for the current schema.
  8. Automatic Sequence Handling for Eloquent Models

    master

    The OracleProcessor supports automatic primary key generation via sequences for Eloquent models. If an Eloquent model has the $incrementing property set to true and a $sequence property defined, the processor will automatically fetch the next value from the Oracle sequence and include it in the insert values if it is not already provided.

    class User extends Model
    {
        protected $incrementing = true;
        protected $sequence = 'user_seq'; // The name of the Oracle sequence
    }