Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
RunSqlQuery
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Console\Commands\Queries;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\Facades\File;
8
9class RunSqlQuery extends Command
10{
11    protected $signature = 'query:run {file}';
12
13    protected $description = 'Executes an SQL query from a file';
14
15    public function handle(): void
16    {
17        $file = $this->argument('file');
18
19        // Check if the file exists
20        if (!File::exists($file)) {
21            $this->error('The specified file does not exist.');
22            return;
23        }
24
25        // Read the SQL query from the file
26        $query = File::get($file);
27
28        // Execute the SQL query
29        try {
30            DB::statement($query);
31            $this->info('SQL query executed successfully.');
32        } catch (\Exception $e) {
33            $this->error('An error occurred while executing the SQL query: ' . $e->getMessage());
34        }
35    }
36}