Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible to use Laravel 4 without a Default Database

I'm creating an application that reads information from a number of different databases, but doesn't actually have its own database, as there is no information being written anywhere.

Basically, a user selects a record and a type, and the application will generate a .pdf file based on their choices. I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. Is there a way to tell Laravel not to connect to a database? I've tried a few things (all in app/config/database.php), first being:

'default' => NULL,
// and 
//'default' => '',

Which both return:

Undefined index: driver

I've also tried:

'default' => 'none',

'connections' => array(
    'none' => array(
        'driver' => '',
        'host' => '',
        ...
     ),
 ),

which in turn returns:

Unsupported driver []

Unsupported host[]

...

And lastly setting 'default' => '', which returns:

Database [] not configured.

I've found ways to use Laravel's models without a database connection, but not actually Laravel itself.

Edit:

Setting the connection to an existing mysql connection and not selecting a database "works":

'default' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => '',
    'username'  => '****',
    'password'  => '****',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

but I'm not sure if that's the right way to do it. It feels like a work-around and not an actual solution.

like image 281
Tim Lewis Avatar asked Apr 23 '15 15:04

Tim Lewis


1 Answers

I would do the following:

'driver' => 'sqlite',

and for the sqlite connection settings

'sqlite' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
    'prefix' => '',
]

That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.

You could also remove the database service provider from your app.php config file:

    'Illuminate\Cookie\CookieServiceProvider',
    //'Illuminate\Database\DatabaseServiceProvider',
    'Illuminate\Encryption\EncryptionServiceProvider',

and the facades for Fluent and Eloquent in the same file:

    'Crypt'     => 'Illuminate\Support\Facades\Crypt',
    //'DB'        => 'Illuminate\Support\Facades\DB',
    //'Eloquent'  => 'Illuminate\Database\Eloquent\Model',
    'Event'     => 'Illuminate\Support\Facades\Event',

which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.

like image 181
Andrew Willis Avatar answered Sep 23 '22 07:09

Andrew Willis