Skip to content

Commit

Permalink
Fix scope not working with withTrashed (#5195)
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph Schweppe <info@cschweppe.de>
  • Loading branch information
MrCrayon and emptynick committed Jan 28, 2021
1 parent 1732bee commit e0d3b00
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/Http/Controllers/VoyagerBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,16 @@ public function show(Request $request, $id)

if (strlen($dataType->model_name) != 0) {
$model = app($dataType->model_name);
$query = $model->query();

// Use withTrashed() if model uses SoftDeletes and if toggle is selected
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$model = $model->withTrashed();
$query = $query->withTrashed();
}
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$model = $model->{$dataType->scope}();
$query = $query->{$dataType->scope}();
}
$dataTypeContent = call_user_func([$model, 'findOrFail'], $id);
$dataTypeContent = call_user_func([$query, 'findOrFail'], $id);
if ($dataTypeContent->deleted_at) {
$isSoftDeleted = true;
}
Expand Down Expand Up @@ -265,15 +266,16 @@ public function edit(Request $request, $id)

if (strlen($dataType->model_name) != 0) {
$model = app($dataType->model_name);
$query = $model->query();

// Use withTrashed() if model uses SoftDeletes and if toggle is selected
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$model = $model->withTrashed();
$query = $query->withTrashed();
}
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$model = $model->{$dataType->scope}();
$query = $query->{$dataType->scope}();
}
$dataTypeContent = call_user_func([$model, 'findOrFail'], $id);
$dataTypeContent = call_user_func([$query, 'findOrFail'], $id);
} else {
// If Model doest exist, get data from table name
$dataTypeContent = DB::table($dataType->name)->where('id', $id)->first();
Expand Down Expand Up @@ -315,15 +317,16 @@ public function update(Request $request, $id)
$id = $id instanceof \Illuminate\Database\Eloquent\Model ? $id->{$id->getKeyName()} : $id;

$model = app($dataType->model_name);
$query = $model->query();
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$model = $model->{$dataType->scope}();
$query = $query->{$dataType->scope}();
}
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$data = $model->withTrashed()->findOrFail($id);
} else {
$data = $model->findOrFail($id);
$query = $query->withTrashed();
}

$data = $query->findOrFail($id);

// Check permission
$this->authorize('edit', $data);

Expand Down Expand Up @@ -508,14 +511,15 @@ public function restore(Request $request, $id)
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

// Check permission
$this->authorize('delete', app($dataType->model_name));
$model = app($dataType->model_name);
$this->authorize('delete', $model);

// Get record
$model = call_user_func([$dataType->model_name, 'withTrashed']);
$query = $model->withTrashed();
if ($dataType->scope && $dataType->scope != '' && method_exists($model, 'scope'.ucfirst($dataType->scope))) {
$model = $model->{$dataType->scope}();
$query = $query->{$dataType->scope}();
}
$data = $model->findOrFail($id);
$data = $query->findOrFail($id);

$displayName = $dataType->getTranslatedAttribute('display_name_singular');

Expand Down Expand Up @@ -783,10 +787,11 @@ public function order(Request $request)
}

$model = app($dataType->model_name);
$query = $model->query();
if ($model && in_array(SoftDeletes::class, class_uses_recursive($model))) {
$model = $model->withTrashed();
$query = $query->withTrashed();
}
$results = $model->orderBy($dataType->order_column, $dataType->order_direction)->get();
$results = $query->orderBy($dataType->order_column, $dataType->order_direction)->get();

$display_column = $dataType->order_display_column;

Expand Down

0 comments on commit e0d3b00

Please sign in to comment.