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

Handle cascaded delete constraints #184 #185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function getMetadata($code)
return null;
}

protected static function boot()
{
parent::boot();

static::deleting(function ($taxonomyTerm) {
$taxonomyTerm->children()->delete();
});
}

public static function getHierarchicalPath($id)
{
$term = TaxonomyTerm::find($id);
Expand Down Expand Up @@ -139,4 +148,4 @@ protected static function newFactory()
{
return TaxonomyTermFactory::new();
}
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Domains\Taxonomy\Models\Taxonomy;
use App\Domains\Taxonomy\Models\TaxonomyTerm;

class TaxonomyController extends Controller
{
Expand Down Expand Up @@ -115,7 +116,8 @@ public function update(Request $request, Taxonomy $taxonomy)
*/
public function delete(Taxonomy $taxonomy)
{
return view('backend.taxonomy.delete', compact('taxonomy'));
$terms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id)->get();
return view('backend.taxonomy.delete', compact('taxonomy', 'terms'));
}


Expand All @@ -128,6 +130,12 @@ public function delete(Taxonomy $taxonomy)
public function destroy(Taxonomy $taxonomy)
{
try {
$terms = TaxonomyTerm::where('taxonomy_id', $taxonomy->id)->get();
if ($terms->count() > 0) {
return redirect()->route('dashboard.taxonomy.index')
->withErrors('Can not delete the Taxonomy as it already has associated Taxonomy Terms. Please reassign or delete those first.');
}

$taxonomy->delete();
return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy was deleted !');
} catch (\Exception $ex) {
Expand Down
52 changes: 26 additions & 26 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 25 additions & 9 deletions resources/views/backend/taxonomy/delete.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,34 @@
<p>Are you sure you want to delete
<strong><i>"{{ $taxonomy->name }}"</i></strong> ?
</p>
<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')),
'method' => 'delete',
'class' => 'container',
]) !!}

@if ($terms->count() > 0)
<p>The following terms are linked to this Taxonomy. Deletion is not permitted until these terms are
reassigned or deleted.</p>
<ul>
@foreach ($terms as $term)
<li>
<a href="{{ route('dashboard.taxonomy.terms.edit', compact('taxonomy', 'term')) }}">
{{ $term->name }} ({{ $term->code }})
</a>
</li>
@endforeach
</ul>
<a href="{{ route('dashboard.taxonomy.index') }}" class="btn btn-light mr-2">Back</a>
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
@else
<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.destroy', compact('taxonomy')),
'method' => 'delete',
'class' => 'container',
]) !!}

<a href="{{ route('dashboard.taxonomy.index') }}" class="btn btn-light mr-2">Back</a>
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</div>
@endif

{!! Form::close() !!}
</div>
</x-slot>
</x-backend.card>
</div>
Expand Down
14 changes: 14 additions & 0 deletions resources/views/backend/taxonomy/terms/delete.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
<p>Are you sure you want to delete
<strong><i>"{{ $term->name }}"</i></strong> ?
</p>

@if ($term->children()->count() > 0)
<p>The following terms are linked to this Taxonomy Term, and will be deleted with this. </p>
<ul>
@foreach ($term->children()->get() as $childTerm)
<li>
<a href="{{ route('dashboard.taxonomy.terms.edit', ['taxonomy', 'childTerm']) }}">
{{ $childTerm->name }} ({{ $childTerm->code }})
</a>
</li>
@endforeach
</ul>
@endif

<div class="d-flex">
{!! Form::open([
'url' => route('dashboard.taxonomy.terms.destroy', ['taxonomy' => $taxonomy, 'term' => $term]),
Expand Down
Loading