Skip to content

Instantly share code, notes, and snippets.

@primeinc
Created January 20, 2023 15:32
Show Gist options
  • Save primeinc/f0555afcb238b2179c230fca3de5132e to your computer and use it in GitHub Desktop.
Save primeinc/f0555afcb238b2179c230fca3de5132e to your computer and use it in GitHub Desktop.
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$sqlFileName = "thecsg_sam.sql";
$sqlFilePath = Storage::path($sqlFileName); // You must change this one, its depend on your mysql bin.
$db_bin = "/usr/bin";
if (Storage::disk('local')->exists($sqlFilePath)) {
try {
$fileContent = Storage::disk('local')->get($sqlFileName);
} catch (\Exception $e) {
throw new \Exception("Couldn't get contents: {$sqlFilePath} " . $e->getMessage());
}
if (!$fileContent) {
throw new \Exception("SQL File is empty: {$sqlFilePath}");
}
$updatedContent = preg_replace('/CREATE TABLE.*;/s', '', $fileContent);
Storage::disk('local')->put($sqlFileName, $updatedContent);
} else {
throw new \Exception("SQL File not found: {$sqlFilePath}");
}
// PDO Credentials
$db = [
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE')
];
dd("{$db_bin}/mysql --user={$db['username']} --password={$db['password']} --host={$db['host']} --database {$db['database']} < $sqlFilePath");
// exec("{$db_bin}/mysql --user={$db['username']} --password={$db['password']} --host={$db['host']} --database {$db['database']} < $sql");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment