A
A
Alexander Ablizin2016-07-07 13:29:58
Laravel
Alexander Ablizin, 2016-07-07 13:29:58

How to dynamically create SQLite databases in Laravel and use them with QB and ORM?

Sorry, this seems more like nonsense, but... If I want to create many SQLite databases along the way and then use them using QueryBuilder or ORM in Laravel, how can I do this?
Those. e.g. $basename = 'base1';
I create a database: $db = sqlite_open("mypath/$basename.sqlite");
How to connect to it and make queries using QueryBuilder or Eloquent ORM?
Please direct where to read or how to google?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ablizin, 2016-07-12
@mcmraak

Everything turned out to be very simple (because Laravel is very cool!)
We connect facades:
use Config;
use Schema;
Further in the method:

$db_name = 'mybase';
$bd_path = "../txt/$db_name.sqlite";
        
# Указываем путь и создаём файл базы если его нет
if(!file_exists($bd_path)){
    file_put_contents($bd_path, '');
}
        
# Динамически создаём конфигурацию подключения
Config::set('database.connections.'.$db_name, array(
    'driver' => 'sqlite',
    'database' => $bd_path,
));
        
# Используя созданное подключение создаём таблицы       
Schema::connection($db_name)->create('mytable', function($table)
{
    $table->increments('id');
    $table->string('name');
});

And so on ...
ZY . Hi cyclists :)

S
Stanislav Pochepko, 2016-07-07
@DJZT

For models, you can specify which database connection to use for queries. Read the Eloquent laravel documentation. But if you have such a need, I think something is wrong with your architecture. Either you do a cool service :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question