Skip to content

Commit c94643b

Browse files
committed
Use model connection for table checks
1 parent 50d88c2 commit c94643b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/Traits/HasHash.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Codebarista\LaravelEssentials\Traits;
44

5-
use Codebarista\LaravelEssentials\Facades\DB;
5+
use Illuminate\Database\Connection;
66
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Support\Facades\Schema;
87
use Illuminate\Support\Str;
98

109
trait HasHash
@@ -13,16 +12,19 @@ protected static function bootHasHash(): void
1312
{
1413
// create and set a unique hash code for model
1514
static::creating(static function (Model $model) {
15+
$connection = $model->getConnection();
16+
$schema = $connection->getSchemaBuilder();
1617
$table = $model->getTable();
17-
if (Schema::hasColumn($table, 'hash')) {
18-
$column = Schema::getColumnType($table, 'hash', true);
18+
19+
if ($schema->hasColumn($table, 'hash')) {
20+
$column = $schema->getColumnType($table, 'hash', true);
1921
$length = preg_replace('/\D/', '', $column);
20-
$model->setAttribute('hash', self::getUniqueHash($table, $length));
22+
$model->setAttribute('hash', self::getUniqueHash($connection, $table, $length));
2123
}
2224
});
2325
}
2426

25-
protected static function getUniqueHash(string $table, int $length, int $iterations = 0): ?string
27+
protected static function getUniqueHash(Connection $connection, string $table, int $length, int $iterations = 0): ?string
2628
{
2729
$hash = Str::random($length);
2830

@@ -31,7 +33,7 @@ protected static function getUniqueHash(string $table, int $length, int $iterati
3133
return $hash;
3234
}
3335

34-
if (DB::table($table)->where('hash', $hash)->exists()) {
36+
if ($connection->table($table)->where('hash', $hash)->exists()) {
3537
return self::getUniqueHash($table, $length, $iterations + 1);
3638
}
3739

0 commit comments

Comments
 (0)