Skip to content

Commit 4f37d8a

Browse files
authored
Updating raw DB queries to include DB prefix.
Noticed my CRUD queries were throwing errors and realized the queries were not taking the database prefix set in my database.php config file. Added in lines to include that.
1 parent a9a7c76 commit 4f37d8a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/CrudTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DB;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Config;
78

89
trait CrudTrait
910
{
@@ -16,7 +17,7 @@ trait CrudTrait
1617
public static function getPossibleEnumValues($field_name)
1718
{
1819
$instance = new static(); // create an instance of the model to be able to get the table name
19-
$type = DB::select(DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"'))[0]->Type;
20+
$type = DB::select(DB::raw('SHOW COLUMNS FROM '.Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable().' WHERE Field = "'.$field_name.'"'))[0]->Type;
2021
preg_match('/^enum\((.*)\)$/', $type, $matches);
2122
$enum = [];
2223
foreach (explode(',', $matches[1]) as $value) {
@@ -29,7 +30,7 @@ public static function getPossibleEnumValues($field_name)
2930
public static function isColumnNullable($column_name)
3031
{
3132
$instance = new static(); // create an instance of the model to be able to get the table name
32-
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0];
33+
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0];
3334

3435
return $answer->IS_NULLABLE === 'YES';
3536
}

0 commit comments

Comments
 (0)