After a user register. i want to create automatically a new database, with some kind of a script. So i need to write some kind of a script that creates automatically a db_name and also recognizes the user with the db_name
I'am currently saving db_name in my user database/migration.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('db_name')->nullable();
$table->timestamps();
});
i'am using middleware to connect to a database dynamic(db_name) works good!
public function handle(Request $request, Closure $next)
{
$user = $request->user();
Config::set('database.connections.mysql.database', $user->db_name);
DB::purge('mysql');
DB::reconnect('mysql');
return $next($request);
}
}
and now i need to find a way to do this automatically creat the db_name automatically. also some kind of way to check if to user already had a db_name. maybe with laravel job?
help me please ;)