Hey guys i'am trying to update my db_name but to prevent error's i'am trying Database: Query Builder
i'am lost can someone help me please? Thank you!
public function handle()
{
$data = DB::table('users')
->select('id','name')
->where('db_name', null)
->limit(10);
// ->update(['db_name' => DB::raw("CONCAT(name, '_', id)")]);// i normally use it here
foreach ( $data as $user ) {
$name = preg_replace("/[^a-zA-Z0-9]/", "", $user->name);
$name = substr($name, 0, 8);
$new_dbname = $name + $user->id;
}
// IF NOT EXISTS!! i'am getting a lot of error here.
$db = CREATE NEW DB IF NOT EXISTS::$new_dbname;
if ( $db->errors !== true ) {
DB::update('db_name', $new_dbname )->where('id',$user->id)->isnull('db_name');
}
}
Does anyone knows, how to create a new DB if not exist? is it possible? i now you can create a Comand for it using the command line but i want to. integrate it in my code above. like:
// de IF NOT EXISTS is wel belangrijk - anders krijg je een fout
$db = CREATE NEW DB IF NOT EXISTS::$new_dbname
If it's not possible i will stop working on it. I already created a createDatabaseCommand. makes it possible to create a database using the command line. but i want to be able to create a DB if not Exist and update my colmn . Thank you
no, i already solved that. i just want to be able to create a DB if it doesn't exist. with a script code wittout creating it with the command line or phpmyadmin
public function index() {
$userName = 'bigboytr'; // Your Database name to be created
DB::statement("CREATE DATABASE $userName");
}
but it's in the controller. Can i implement it like below? below
// IF NOT EXISTS!! i'am getting a lot of error here.
$db = CREATE NEW DB IF NOT EXISTS::$new_dbname;
if ( $db->errors !== true ) {
DB::update('db_name', $new_dbname )->where('id',$user->id)->isnull('db_name');
}
}