PHP 에 OCI extension 을 설치하고 Laravel-OCI8 패키지를 설정하면 Query Builder 나 Eloquent ORM에서도 바로 Oracle 과 연결하여 database 쿼리를 수행할 수 있다.

사전 작업

PHP용 OCI 드라이버가 설치되어야 한다.

위 문서에 있는 SELinux 정책 변경도 사전에 적용해 두자.


설치

  1. composer.json 에 laravel용 오라클 패키지인 yajra/laravel-oci8 를 추가.

    {
        "require": {
            "yajra/laravel-oci8": "^8"
        }
    }
    CODE


    컴포저 업데이트 실행

    composer update
    CODE
  2. 또는 명령행에서 실행

    composer require yajra/laravel-oci8:^8
    CODE
  3. OCI Provider 를 laravel 의 service provider 에 등록해 줘야 함. config/app.php 를 열어서 'provides' 에 다음 내용 추가

    'providers' => [
              // 추가
            Yajra\Oci8\Oci8ServiceProvider::class,
        ],
    PHP
  4. config/oracle.php 이 생기도록 vendor publishing 실행

    php artisan vendor:publish --tag=oracle
    CODE
  5. app/database.php 에 DB 연결 정보를 추가한다.

    database.php

    <?php
    return [
        /*
        |--------------------------------------------------------------------------
        | Default Database Connection Name
        |--------------------------------------------------------------------------
        |
        | Here you may specify which of the database connections below you wish
        | to use as your default connection for all database work. Of course
        | you may use many connections at once using the Database library.
        |
        */
    
    
    'default' => 'oracle',
    'connections' => [
            'oracle' => [
                'driver' => 'oracle',
                'host' => env('ORACLE_HOST'),
                'port' => env('ORACLE_PORT', 1521),
                'database' =>  env('ORACLE_SID'),
                'username' => env('ORACLE_USERNAME'),
                'password' => env('ORACLE_PASSWORD'),
                'charset' => env('ORACLE_CHARSET','AL32UTF8'),
                'prefix' => '',
            ],
            'sqlite' => [
                'driver'   => 'sqlite',
                'database' => __DIR__.'/../database/production.sqlite',
                'prefix'   => '',
            ],
            'mysql' => [
                'driver'    => 'mysql',
                'host'      => 'localhost',
                'database'  => 'forge',
                'username'  => 'forge',
                'password'  => '',
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
            ],
    ],
    PHP
  6. .env 파일에 연결 정보를 설정한다.

    ORACLE_HOST=myoracle.example.com
    ORACLE_PORT=1521
    ORACLE_SID=mySid
    ORACLE_USERNAME=scott
    ORACLE_PASSWORD=tiger
    ORACLE_CHARSET=AL32UTF8
    CODE

테스트

간단하게 기존 테이블에 맞는 모델(예: Users 테이블이 있을 경우 User 모델)을 생성하고 테스트 진행

php artisan make:model User
CODE


라우트 파일

routes.php

Route::get('oratest', function($id) {
	$u = \App\User::find($id)->get();
 
	return $u;
});
PHP

웹 브라우저로 http://localhost:8000/oratest/3 에 연결하여 동작 여부 확인


만약 다음과 같은 에러가 발생한다면 서비스 프로바이더(yajra\Oci8\Oci8ServiceProvider::class)config/app.php 에 등록했는지 확인.

InvalidArgumentException with message 'Unsupported driver [oracle]'
CODE


사용

blob 

  1. blob 컬럼을 처리할 경우 다음과 같이 모델이 Model 클래스가 아닌 OracleEloquent  를 상속받도록 수정
  2. property 는 $binaries 배열에 blob컬럼 이름을 설정(아래 예제는 content 컬럼이 blob)
use yajra\Oci8\Eloquent\OracleEloquent as Eloquent;
 
class Post extends Eloquent {

    // define binary/blob fields
    protected $binaries = ['content'];

    // define the sequence name used for incrementing
    // default value would be {table}_{primaryKey}_seq if not set
    protected $sequence = null;

}
CODE



같이 보기