File tree 2 files changed +84
-0
lines changed
2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " illegal/laravel-utils" ,
3
+ "description" : " Laravel Utils is a collection of useful tools for Laravel." ,
4
+ "type" : " library" ,
5
+ "license" : " MIT" ,
6
+ "authors" : [
7
+ {
8
+ "name" : " Vincenzo Petrucci" ,
9
+
10
+ "role" : " Maintainer"
11
+ }
12
+ ],
13
+ "minimum-stability" : " stable" ,
14
+ "require" : {
15
+ "php" : " ^8.1" ,
16
+ "laravel/framework" : " ^9|^10"
17
+ },
18
+ "require-dev" : {
19
+ },
20
+ "autoload" : {
21
+ "psr-4" : {
22
+ "Illegal\\ LaravelUtils\\ " : " src/"
23
+ }
24
+ },
25
+ "autoload-dev" : {
26
+ "psr-4" : {
27
+ "Illegal\\ LaravelUtils\\ Tests\\ " : " tests/"
28
+ }
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illegal \LaravelUtils \Contracts ;
4
+
5
+ use Illuminate \Support \Str ;
6
+
7
+ trait HasPrefix
8
+ {
9
+ /**
10
+ * The table name will be cached here the first time is called.
11
+ * Subsequent calls will return the cached value.
12
+ */
13
+ protected ?string $ tableNameCache = null ;
14
+
15
+ /**
16
+ * Returns the prefix, override this function if you need
17
+ * a programmatic way of setting the prefix.
18
+ *
19
+ * If you need a "static" prefix, you can set it in your model via the $prefix property.
20
+ */
21
+ protected function getPrefix (): string
22
+ {
23
+ return $ this ->prefix ?? '' ;
24
+ }
25
+
26
+ /**
27
+ * Returns the table name with the prefix.
28
+ * Overrides the default getTable() method.
29
+ */
30
+ public function getTable (): string
31
+ {
32
+ if (filled ($ this ->tableNameCache )) {
33
+ return $ this ->tableNameCache ;
34
+ }
35
+
36
+ return $ this ->tableNameCache = $ this ->getPrefix () . Str::snake (Str::pluralStudly (class_basename ($ this )));
37
+ }
38
+
39
+ /**
40
+ * A static method to get the table name.
41
+ */
42
+ public static function getTableName (): string
43
+ {
44
+ return ( new static )->getTable ();
45
+ }
46
+
47
+ /**
48
+ * This method returns the table name with the field name.
49
+ */
50
+ public static function getField (string $ field ): string
51
+ {
52
+ return self ::getTableName () . '. ' . $ field ;
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments