9
9
use Illuminate \Database \Eloquent \Relations \HasOneOrMany ;
10
10
use Illuminate \Database \Eloquent \Relations \HasOneThrough ;
11
11
use Illuminate \Database \Eloquent \Relations \MorphTo ;
12
+ use Illuminate \Database \Eloquent \Relations \Relation ;
12
13
use Yajra \DataTables \Exceptions \Exception ;
13
14
14
15
/**
@@ -19,22 +20,15 @@ class EloquentDataTable extends QueryDataTable
19
20
/**
20
21
* EloquentEngine constructor.
21
22
*
22
- * @param \Illuminate\Database\Eloquent\Model|EloquentBuilder $model
23
- *
24
- * @throws \Yajra\DataTables\Exceptions\Exception
23
+ * @param Model|EloquentBuilder $model
25
24
*/
26
- public function __construct ($ model )
25
+ public function __construct (Model | EloquentBuilder $ model )
27
26
{
28
- switch ($ model ) {
29
- case $ model instanceof Model:
30
- $ builder = $ model ->newQuery ();
31
- break ;
32
- case $ model instanceof EloquentBuilder:
33
- $ builder = $ model ;
34
- break ;
35
- default :
36
- throw new Exception ('Invalid model type. Must be an instance of Eloquent Model or Eloquent Builder. ' );
37
- }
27
+ $ builder = match (true ) {
28
+ $ model instanceof Model => $ model ->newQuery (),
29
+ $ model instanceof Relation => $ model ->getQuery (),
30
+ $ model instanceof EloquentBuilder => $ model ,
31
+ };
38
32
39
33
parent ::__construct ($ builder ->getQuery ());
40
34
@@ -100,10 +94,13 @@ protected function compileQuerySearch($query, string $column, string $keyword, s
100
94
}
101
95
102
96
if ($ this ->isMorphRelation ($ relation )) {
103
- $ query ->{$ boolean .'WhereHasMorph ' }($ relation , '* ' ,
97
+ $ query ->{$ boolean .'WhereHasMorph ' }(
98
+ $ relation ,
99
+ '* ' ,
104
100
function (EloquentBuilder $ query ) use ($ newColumn , $ keyword ) {
105
101
parent ::compileQuerySearch ($ query , $ newColumn , $ keyword , '' );
106
- });
102
+ }
103
+ );
107
104
} else {
108
105
$ query ->{$ boolean .'WhereHas ' }($ relation , function (EloquentBuilder $ query ) use ($ newColumn , $ keyword ) {
109
106
parent ::compileQuerySearch ($ query , $ newColumn , $ keyword , '' );
0 commit comments