Skip to content

Commit

Permalink
Merge pull request #314 from range-of-motion/313-fix-not-being-able-t…
Browse files Browse the repository at this point in the history
…o-view-budgets-page-after-deleting-tag

Fix not being able to view budgets page after deleting tag
  • Loading branch information
range-of-motion authored Jun 29, 2021
2 parents 8f44517 + 5924380 commit e5c258c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class Tag extends Model
];

// Relations
public function budgets()
{
return $this->hasMany(Budget::class);
}

public function spendings()
{
return $this->hasMany(Spending::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use App\Models\Budget;
use App\Models\Tag;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DeleteBudgetsWithoutTags extends Migration
{
public function up(): void
{
foreach (Budget::all() as $budget) {
$hasStrayTag = $budget->tag_id && !$budget->tag;

if ($hasStrayTag) {
$budget->delete();
}
}
}

public function down(): void
{
//
}
}
2 changes: 1 addition & 1 deletion resources/views/tags/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</a>
</div>
<div class="row__column row__column--compact ml-2">
@if ($tag->spendings->count())
@if ($tag->budgets->count() || $tag->spendings->count())
<i class="fas fa-trash-alt"></i>
@else
<form method="POST" action="/tags/{{ $tag->id }}">
Expand Down

0 comments on commit e5c258c

Please sign in to comment.