Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scope not working with withTrashed #5195

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -497,14 +500,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 @@ -772,10 +776,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