Skip to content

Commit

Permalink
Merge pull request #85 from Team-Nifty-GmbH/add-get-model-method
Browse files Browse the repository at this point in the history
add getModel method
  • Loading branch information
patrickweh authored Mar 5, 2024
2 parents df2bc35 + dfcf88d commit f5dae6b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
25 changes: 15 additions & 10 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ protected function forceRender(): void
store($this)->set('skipRender', false);
}

protected function getModel(): string
{
return $this->model;
}

#[Renderless]
public function getConfig(): array
{
Expand Down Expand Up @@ -207,7 +212,7 @@ protected function getEnabledCols(): array
public function getAvailableCols(): array
{
$availableCols = $this->availableCols === ['*']
? ModelInfo::forModel($this->model)->attributes->pluck('name')->toArray()
? ModelInfo::forModel($this->getModel())->attributes->pluck('name')->toArray()
: $this->availableCols;

return array_values(
Expand Down Expand Up @@ -254,7 +259,7 @@ public function getColLabels(?array $cols = null): array

protected function getTableFields(): \Illuminate\Support\Collection
{
return ModelInfo::forModel($this->model)
return ModelInfo::forModel($this->getModel())
->attributes
->filter(function (Attribute $attribute) {
return ! $attribute->virtual
Expand All @@ -273,7 +278,7 @@ private function whereIn(Builder $builder, array $filter): Builder

protected function getIncludedRelations(): array
{
$baseModelInfo = ModelInfo::forModel($this->model);
$baseModelInfo = ModelInfo::forModel($this->getModel());
$loadedRelations = [];
foreach ($this->enabledCols as $enabledCol) {
if (str_contains($enabledCol, '.')) {
Expand All @@ -293,7 +298,7 @@ protected function getIncludedRelations(): array
'loaded_as' => $enabledCol,
];
$loadedRelations[$path] = [
'model' => $relation?->related ?? $this->model,
'model' => $relation?->related ?? $this->getModel(),
'loaded_columns' => $loadedColumns,
'type' => $relation?->type,
];
Expand Down Expand Up @@ -444,7 +449,7 @@ public function loadData(): void
protected function buildSearch(): Builder
{
/** @var Model $model */
$model = $this->model;
$model = $this->getModel();

foreach ($this->getAggregatableRelationCols() as $aggregatableRelationCol) {
$this->aggregatableRelationCols[] = $aggregatableRelationCol->alias;
Expand Down Expand Up @@ -492,7 +497,7 @@ protected function buildSearch(): Builder

protected function getScoutSearch(): \Laravel\Scout\Builder
{
return $this->model::search($this->search);
return $this->getModel()::search($this->search);
}

private function with(Builder $builder, array $filter): Builder
Expand Down Expand Up @@ -758,7 +763,7 @@ protected function itemToArray($item): array
}
}

$itemArray['href'] = in_array(InteractsWithDataTables::class, class_implements($this->model))
$itemArray['href'] = in_array(InteractsWithDataTables::class, class_implements($this->getModel()))
&& ! $this->hasNoRedirect
&& method_exists($item, 'getUrl')
? $item->getUrl()
Expand Down Expand Up @@ -795,7 +800,7 @@ public function mount(): void
$this->colLabels = $this->getColLabels();

if (! $this->modelKeyName || ! $this->modelTable) {
$model = (new $this->model);
$model = app($this->getModel());
$this->modelKeyName = $this->modelKeyName ?: $model->getKeyName();
$this->modelTable = $this->modelTable ?: $model->getTable();
}
Expand Down Expand Up @@ -834,7 +839,7 @@ protected function getViewData(): array
'rowActions' => $this->getRowActions(),
'tableActions' => $this->getTableActions(),
'selectedActions' => $this->getSelectedActions(),
'modelName' => Str::headline(class_basename($this->model)),
'modelName' => Str::headline(class_basename($this->getModel())),
'showFilterInputs' => $this->showFilterInputs,
'layout' => $this->getLayout(),
'useWireNavigate' => $this->useWireNavigate,
Expand All @@ -847,7 +852,7 @@ protected function getViewData(): array
protected function getIsSearchable(): bool
{
return is_null($this->isSearchable)
? in_array(Searchable::class, class_uses_recursive($this->model))
? in_array(Searchable::class, class_uses_recursive($this->getModel()))
: $this->isSearchable;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/DataTables/SupportsExporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function export(array $columns = []): Response|BinaryFileResponse
$query = $this->buildSearch();

return (new DataTableExport($query, array_filter($columns)))
->download(class_basename($this->model) . '_' . now()->toDateTimeLocalString('minute') . '.xlsx');
->download(class_basename($this->getModel()) . '_' . now()->toDateTimeLocalString('minute') . '.xlsx');
}
}
20 changes: 10 additions & 10 deletions src/Traits/DataTables/SupportsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait SupportsRelations

public function mountSupportsRelations(): void
{
$this->loadRelation($this->model, null);
$this->loadRelation($this->getModel(), null);
}

#[Renderless]
Expand All @@ -45,7 +45,7 @@ public function loadRelation(?string $model = null, ?string $relationName = null
return;
}

$model = $model ?: $this->model;
$model = $model ?: $this->getModel();

$this->loadedPath = $relationName ? ($this->loadedPath ? $this->loadedPath . '.' : null) . $relationName : null;

Expand Down Expand Up @@ -91,7 +91,7 @@ public function loadRelation(?string $model = null, ?string $relationName = null
}, $selectedCols);

Cache::put(
'relation-tree-widget.' . $this->loadedPath ?? $this->model,
'relation-tree-widget.' . $this->loadedPath ?? $this->getModel(),
[
'cols' => $this->selectedCols,
'relations' => $this->selectedRelations,
Expand All @@ -108,7 +108,7 @@ public function loadSlug(?string $path = null): void
}

$this->loadedPath = $path;
$data = Cache::get('relation-tree-widget.' . $path ?? $this->model);
$data = Cache::get('relation-tree-widget.' . $path ?? $this->getModel());

$this->selectedCols = $data['cols'];
$this->selectedRelations = $data['relations'];
Expand All @@ -123,7 +123,7 @@ public function getFilterableColumns(?string $name = null): array

public function getRelationTableCols(?string $relationName = null): array
{
$modelInfo = ModelInfo::forModel($this->model);
$modelInfo = ModelInfo::forModel($this->getModel());

if ($relationName) {
$modelInfo = ModelInfo::forModel($modelInfo->relation($relationName)->related);
Expand Down Expand Up @@ -151,7 +151,7 @@ protected function constructWith(): array
//return $cached[$cacheKey];
}

$modelBase = new $this->model;
$modelBase = app($this->getModel());
$with = ['__root__' => []];
$modelInfos = [];
$filterable = [];
Expand Down Expand Up @@ -197,11 +197,11 @@ protected function constructWith(): array

// check if the field is virtual or has a value list to filter
// if $model is empty, we are on the root model
if ($modelInfos[$model ? get_class($model) : $this->model] ?? false) {
$modelInfo = $modelInfos[$model ? get_class($model) : $this->model];
if ($modelInfos[$model ? get_class($model) : $this->getModel()] ?? false) {
$modelInfo = $modelInfos[$model ? get_class($model) : $this->getModel()];
} else {
$modelInfo = ModelInfo::forModel($model ? get_class($model) : $this->model);
$modelInfos[$model ? get_class($model) : $this->model] = $modelInfo;
$modelInfo = ModelInfo::forModel($model ? get_class($model) : $this->getModel());
$modelInfos[$model ? get_class($model) : $this->getModel()] = $modelInfo;
}

$attributeInfo = $modelInfo->attribute($fieldName);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/DataTables/SupportsSelecting.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function getSelectedValues(): array

protected function getSelectedModels(): Collection
{
return $this->model::query()
return $this->getModel()::query()
->whereIntegerInRaw($this->modelKeyName, $this->getSelectedValues())
->get();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/HasEloquentListeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public function getPaginator(LengthAwarePaginator $paginator): LengthAwarePagina
}

if (
in_array(BroadcastsEvents::class, class_uses_recursive($this->model))
in_array(BroadcastsEvents::class, class_uses_recursive($this->getModel()))
&& $paginator->currentPage() === 1
) {
$this->broadcastChannels['created'] = $this->model::getBroadcastChannel(true);
$this->broadcastChannels['created'] = $this->getModel()::getBroadcastChannel(true);
}

return $paginator;
}

public function eloquentEventOccurred(string $event, array $data): void
{
$event = str_replace('.' . class_basename($this->model), 'echo', $event);
$event = str_replace('.' . class_basename($this->getModel()), 'echo', $event);

$this->{$event}($data);
}

public function echoUpdated($eventData): void
{
$model = $this->getBuilder($this->model::query()->whereKey($eventData['model'][$this->modelKeyName]))->first();
$model = $this->getBuilder($this->getModel()::query()->whereKey($eventData['model'][$this->modelKeyName]))->first();

$item = $this->itemToArray($model);
$data = \Arr::keyBy($this->data['data'], $this->modelKeyName);
Expand All @@ -46,7 +46,7 @@ public function echoUpdated($eventData): void

public function echoCreated($eventData): void
{
$model = $this->getBuilder($this->model::query()->whereKey($eventData['model'][$this->modelKeyName]))->first();
$model = $this->getBuilder($this->getModel()::query()->whereKey($eventData['model'][$this->modelKeyName]))->first();
$item = $this->itemToArray($model);

array_unshift($this->data['data'], $item);
Expand Down

0 comments on commit f5dae6b

Please sign in to comment.