To connect a Laravel application to an Oracle database, you need to set up the necessary configurations and install the appropriate packages. Here's a step-by-step guide to help you achieve this:
Step 1: Install the Oracle Database Driver
Install the OCI8 PHP Extension:
For Linux (Ubuntu):
sudo apt-get install php-pear php-dev libaio1 sudo pecl install oci8
During installation, you will be prompted to enter the Oracle Home directory or Instant Client directory. If you have the Oracle Instant Client, enter the directory path where it's installed.
For macOS, if you're using Homebrew:
brew tap InstantClientTap/instantclient brew install instantclient-basic instantclient-sdk sudo pecl install oci8
For Windows:
- Download the Oracle Instant Client from the Oracle website.
- Extract the contents and set up the environment variables for
ORACLE_HOME
andTNS_ADMIN
. - Enable the OCI8 extension in your
php.ini
file.
Install the yajra/laravel-oci8 Package:
This package provides an Oracle DB driver for Laravel.
composer require yajra/laravel-oci8
Step 2: Configure the Database Connection
Update the
.env
File:Add your Oracle database connection details to the
.env
file:DB_CONNECTION=oracle DB_HOST=your_host DB_PORT=your_port DB_DATABASE=your_database DB_USERNAME=your_username DB_PASSWORD=your_password
Update the
config/database.php
File:Add the Oracle database configuration to the
connections
array inconfig/database.php
:'connections' => [ // Other connections... 'oracle' => [ 'driver' => 'oracle', 'tns' => env('DB_TNS', ''), 'host' => env('DB_HOST', ''), 'port' => env('DB_PORT', '1521'), 'database' => env('DB_DATABASE', ''), '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'), ], ],
Set Oracle as the Default Database Connection (Optional):
If you want Oracle to be your default database connection, update the
default
key inconfig/database.php
:'default' => env('DB_CONNECTION', 'oracle'),
Step 3: Test the Connection
Run Migrations:
To test the connection, you can run Laravel migrations:
php artisan migrate
If everything is set up correctly, your migrations should run without any issues, creating the necessary tables in your Oracle database.
Create a Model and Test Queries:
You can create a model and test some queries to ensure that your Laravel application can interact with the Oracle database:
php artisan make:model TestModel
In your controller or a tinker session, try querying the database:
use App\Models\TestModel; $data = TestModel::all(); dd($data);
Conclusion
By following these steps, you can successfully connect your Laravel application to an Oracle database. The yajra/laravel-oci8
package provides the necessary support for Oracle, allowing you to leverage Laravel's Eloquent ORM and other database features with an Oracle database. For more detailed information and advanced configurations, refer to the yajra/laravel-oci8 documentation.
0 Comments