2
2
3
3
namespace Codebarista \LaravelEssentials \Traits ;
4
4
5
- use Codebarista \ LaravelEssentials \ Facades \ DB ;
5
+ use Illuminate \ Database \ Connection ;
6
6
use Illuminate \Database \Eloquent \Model ;
7
- use Illuminate \Support \Facades \Schema ;
8
7
use Illuminate \Support \Str ;
9
8
10
9
trait HasHash
@@ -13,16 +12,19 @@ protected static function bootHasHash(): void
13
12
{
14
13
// create and set a unique hash code for model
15
14
static ::creating (static function (Model $ model ) {
15
+ $ connection = $ model ->getConnection ();
16
+ $ schema = $ connection ->getSchemaBuilder ();
16
17
$ 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 );
19
21
$ length = preg_replace ('/\D/ ' , '' , $ column );
20
- $ model ->setAttribute ('hash ' , self ::getUniqueHash ($ table , $ length ));
22
+ $ model ->setAttribute ('hash ' , self ::getUniqueHash ($ connection , $ table , $ length ));
21
23
}
22
24
});
23
25
}
24
26
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
26
28
{
27
29
$ hash = Str::random ($ length );
28
30
@@ -31,7 +33,7 @@ protected static function getUniqueHash(string $table, int $length, int $iterati
31
33
return $ hash ;
32
34
}
33
35
34
- if (DB :: table ($ table )->where ('hash ' , $ hash )->exists ()) {
36
+ if ($ connection -> table ($ table )->where ('hash ' , $ hash )->exists ()) {
35
37
return self ::getUniqueHash ($ table , $ length , $ iterations + 1 );
36
38
}
37
39
0 commit comments