Answer the question
In order to leave comments, you need to log in
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
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');
});
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 questionAsk a Question
731 491 924 answers to any question