This guide covers the complete process of installing Oracle Database, connecting it with Laravel using the yajra/laravel-oci8 package, and setting up a working environment.
- oracle db (23ai free) : https://download.oracle.com/otn-pub/otn_software/db-express/WINDOWS.X64_238000_free.zip
- download dbevear and make connection to oracle db
- php 8.2
- laravel 9
- pecl: php_oci8_19.dll - https://pecl.php.net/package/oci8/3.4.0/windows
- package yajra/laravel-oci8: "^9"
- Download Oracle Database from the Oracle website
- During installation, note that "SS" will be used as your service name
- Make sure to remember the service password you set during installation
- Complete the installation process following on-screen instructions
After installation, verify the Oracle listener is running and note the port:
lsnrctl status
lsnrctl reload
- Download and install DBeaver from https://dbeaver.io/download/
- Launch DBeaver after installation
- Click "New Database Connection"
- Select "Oracle"
- Enter the following details:
- Host: localhost
- Port: 1521 or 1522 (default port, use the one from lsnrctl status if different). you will find prot also in 'SS'
- Service name: SS (the service name you defined during installation)
- Username: system
- Password: (use the password you set during Oracle installation)
- Test the connection and save it
Connect to Oracle as the system user and execute the following SQL commands in a SQL script:
CREATE USER laravel_user IDENTIFIED BY laravel_password;
GRANT CONNECT, RESOURCE TO laravel_user;
ALTER USER laravel_user QUOTA UNLIMITED ON USERS;
- Click "New Database Connection" again
- Select "Oracle"
- Enter the following details:
- Host: localhost
- Port: 1521 or 1522 you will find prot also in 'SS'
- Service name: SS
- Username: laravel_user
- Password: laravel_password
- Test the connection and save it
Use PECL to install the OCI8 extension:
- Download form https://pecl.php.net/package/oci8/3.4.0/windows
- put into ../php/ext/
When prompted, provide the path to your Oracle Instant Client directory.
Edit your php.ini file to enable the OCI8 extension by adding or uncommenting these lines:
extension=oci8_19
- run command to confirm the extension is loaded
php -m
Restart your web server after making changes to php.ini.
Create a new Laravel project:
composer create-project laravel/laravel:^9.0 laravel-oracle-project
Install the yajra/laravel-oci8 package:
composer require yajra/laravel-oci8:"^9"
Add the Oracle connection details to your config/database.php
file:
'connections' => [
// ... other connections
'oracle' => [
'driver' => 'oracle',
'tns' => '',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1521'),
'database' => env('DB_DATABASE', 'FREEPDB1'),
'service_name' => env('DB_SERVICE_NAME', 'FREEPDB1'),
'username' => env('DB_USERNAME', 'laravel_user'),
'password' => env('DB_PASSWORD', 'laravel_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'),
'dynamic' => [],
],
],
Update your .env
file with Oracle connection details:
DB_CONNECTION=oracle
DB_HOST=localhost
DB_PORT=1521
DB_SERVICE_NAME=FREEPDB1
DB_USERNAME=laravel_user
DB_PASSWORD=laravel_password
Run Laravel migrations to verify the connection:
php artisan migrate
If everything is set up correctly, migrations should run successfully.
-
OCI8 extension not found
- Verify that the extension is properly installed with
php -m
- Check your php.ini file to ensure the extension is enabled
- Verify that the extension is properly installed with
-
Connection issues
- Verify Oracle service is running with
lsnrctl status
- Test the connection using DBeaver or SQL*Plus
- Check firewall settings if connecting to a remote database
- Verify Oracle service is running with
-
Migration errors
- Ensure the laravel_user has the necessary permissions
- Check for syntax differences between MySQL and Oracle in your migrations
-
Test Result
- Migration is working fine
- ORM is working fine
- Make two CRUD operations to verify the connection
- DB facades worked
- sanctum tested