Skip to content

Commit dd85331

Browse files
authored
Added a descriptive exception handler...
...so that people don't get confused in case they forget to extend the base datatable engines.
1 parent d04272d commit dd85331

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/DataTables.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ public function getConfig()
113113
public function query(QueryBuilder $builder): QueryDataTable
114114
{
115115
$dataTable = config('datatables.engines.query');
116-
116+
117+
if (! is_subclass_of($dataTable, QueryDataTable::class)) {
118+
$this->throwInvalidEngineException($dataTable, QueryDataTable::class);
119+
}
120+
117121
return $dataTable::create($builder);
118122
}
119123

@@ -126,7 +130,11 @@ public function query(QueryBuilder $builder): QueryDataTable
126130
public function eloquent(EloquentBuilder $builder): EloquentDataTable
127131
{
128132
$dataTable = config('datatables.engines.eloquent');
129-
133+
134+
if (! is_subclass_of($dataTable, EloquentDataTable::class)) {
135+
$this->throwInvalidEngineException($dataTable, EloquentDataTable::class);
136+
}
137+
130138
return $dataTable::create($builder);
131139
}
132140

@@ -139,7 +147,11 @@ public function eloquent(EloquentBuilder $builder): EloquentDataTable
139147
public function collection($collection): CollectionDataTable
140148
{
141149
$dataTable = config('datatables.engines.collection');
142-
150+
151+
if (! is_subclass_of($dataTable, CollectionDataTable::class)) {
152+
$this->throwInvalidEngineException($dataTable, CollectionDataTable::class);
153+
}
154+
143155
return $dataTable::create($collection);
144156
}
145157

@@ -158,4 +170,15 @@ public function getHtmlBuilder()
158170

159171
return $this->html ?: $this->html = app('datatables.html');
160172
}
173+
174+
/**
175+
* @param string $engine
176+
* @param string $parent
177+
*
178+
* @throws \Yajra\DataTables\Exceptions\Exception
179+
*/
180+
public function throwInvalidEngineException(string $engine, string $parent)
181+
{
182+
throw new Exception("The given datatable engine `{$engine}` is not compatible with `{$parent}`.");
183+
}
161184
}

0 commit comments

Comments
 (0)