diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index 450cf7c9..2f2c82f3 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -6,6 +6,7 @@ use Database\Factories\TaxonomyFactory; use Illuminate\Database\Eloquent\Model; use Spatie\Activitylog\Traits\LogsActivity; +use App\Domains\Taxonomy\Models\TaxonomyTerm; use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Domains\Taxonomy\Models\Traits\Scope\TaxonomyScope; @@ -48,6 +49,11 @@ public function user() return $this->belongsTo(User::class, 'created_by'); } + public function terms() + { + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); + } + /** * Create a new factory instance for the model. * diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index 6a8d8dc3..143b5f73 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -37,6 +37,21 @@ public function user() return $this->belongsTo(User::class, 'created_by'); } + public function taxonomy() + { + return $this->belongsTo(Taxonomy::class, 'taxonomy_id'); + } + + public function parent() + { + return $this->belongsTo(self::class, 'parent_id'); + } + + public function children() + { + return $this->hasMany(self::class, 'parent_id'); + } + /** * Create a new factory instance for the model. * diff --git a/app/Http/Controllers/Backend/TaxonomyTermController.php b/app/Http/Controllers/Backend/TaxonomyTermController.php index 2e74610e..e391de6e 100644 --- a/app/Http/Controllers/Backend/TaxonomyTermController.php +++ b/app/Http/Controllers/Backend/TaxonomyTermController.php @@ -5,23 +5,28 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; use App\Http\Controllers\Controller; +use App\Domains\Taxonomy\Models\Taxonomy; use App\Domains\Taxonomy\Models\TaxonomyTerm; class TaxonomyTermController extends Controller { + public function index(Taxonomy $taxonomy) + { + return view('backend.taxonomy.terms.index', compact('taxonomy')); + } /** * Show the form for creating a new resource. * * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function create() + public function create(Taxonomy $taxonomy) { - try{ - return view('backend.taxonomy.terms.create'); - }catch (\Exception $ex) { + try { + return view('backend.taxonomy.terms.create', compact('taxonomy')); + } catch (\Exception $ex) { Log::error('Failed to load taxonomy terms creation page', ['error' => $ex->getMessage()]); return abort(500); - } + } } /** * Store a newly created resource in storage. @@ -29,9 +34,26 @@ public function create() * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|void */ - public function store(Request $request) + public function store(Request $request, Taxonomy $taxonomy) { + try { + $validatedData = $request->validate([ + 'code' => 'required|unique:taxonomy_terms,code', + 'name' => 'required', + 'metadata' => 'nullable|json', + 'parent_id' => 'nullable|exists:taxonomy_terms,id' + ]); + + $taxonomyTerm = new TaxonomyTerm($validatedData); + $taxonomyTerm->taxonomy_id = $taxonomy->id; + $taxonomyTerm->save(); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy) + ->with('Success', 'Taxonomy term was created successfully!'); + } catch (\Exception $ex) { + Log::error('Failed to create taxonomy term', ['error' => $ex->getMessage()]); + return back()->withInput()->withErrors(['error' => 'Failed to create taxonomy term. Please try again.']); + } } /** * Show the form for editing the specified resource. @@ -39,11 +61,11 @@ public function store(Request $request) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function edit(TaxonomyTerm $taxonomyTerm) + public function edit(Taxonomy $taxonomy, TaxonomyTerm $term) { - try{ - return view('backend.taxonomy.terms.edit', ['taxonomyTerm' => $taxonomyTerm]); - }catch (\Exception $ex) { + try { + return view('backend.taxonomy.terms.edit', compact('taxonomy', 'term')); + } catch (\Exception $ex) { Log::error('Failed to load taxonomy term edit page', ['error' => $ex->getMessage()]); return abort(500); } @@ -55,9 +77,24 @@ public function edit(TaxonomyTerm $taxonomyTerm) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse */ - public function update(Request $request, TaxonomyTerm $taxonomyTerm) + public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term) { + try { + $validatedData = $request->validate([ + 'code' => 'required|unique:taxonomy_terms,code,' . $term->id, + 'name' => 'required', + 'metadata' => 'nullable|json', + 'parent_id' => 'nullable|exists:taxonomy_terms,id' + ]); + + $term->update($validatedData); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy) + ->with('Success', 'Taxonomy term was updated successfully!'); + } catch (\Exception $ex) { + Log::error('Failed to update taxonomy term', ['error' => $ex->getMessage()]); + return back()->withInput()->withErrors(['error' => 'Failed to update taxonomy term. Please try again.']); + } } /** * Confirm to delete the specified resource from storage. @@ -65,26 +102,25 @@ public function update(Request $request, TaxonomyTerm $taxonomyTerm) * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View */ - public function delete(TaxonomyTerm $taxonomyTerm) + public function delete(Taxonomy $taxonomy, TaxonomyTerm $term) { - return view('backend.taxonomy.terms.delete', compact('taxonomyTerm')); + return view('backend.taxonomy.terms.delete', compact('taxonomy', 'term')); } - /** * Remove the specified resource from storage. * * @param \App\Domains\TaxonomyTerm\Models\TaxonomyTerm $taxonomyTerm * @return \Illuminate\Http\RedirectResponse|null */ - public function destroy(TaxonomyTerm $taxonomyTerm) + public function destroy(Taxonomy $taxonomy, TaxonomyTerm $term) { try { - $taxonomyTerm->delete(); - return redirect()->route('dashboard.taxonomy.terms.index')->with('Success', 'Taxonomy term was deleted !'); + $term->delete(); + return redirect()->route('dashboard.taxonomy.terms.index', $taxonomy)->with('Success', 'Taxonomy term was deleted !'); } catch (\Exception $ex) { - Log::error('Failed to delete taxonomy term', ['taxonomyTerm_id' => $taxonomyTerm->id, 'error' => $ex->getMessage()]); - return abort(500); + Log::error('Failed to delete taxonomy term', ['term_id' => $term->id, 'error' => $ex->getMessage()]); + return back()->withErrors(['error' => 'Failed to delete taxonomy term. Please try again.']); } } } \ No newline at end of file diff --git a/app/Http/Livewire/Backend/TaxonomyTable.php b/app/Http/Livewire/Backend/TaxonomyTable.php index 0e2baae4..9028c80e 100644 --- a/app/Http/Livewire/Backend/TaxonomyTable.php +++ b/app/Http/Livewire/Backend/TaxonomyTable.php @@ -36,7 +36,8 @@ public function columns(): array public function query(): Builder { - return Taxonomy::query(); + return Taxonomy::query() + ->with('user'); } public function rowView(): string diff --git a/app/Http/Livewire/Backend/TaxonomyTermTable.php b/app/Http/Livewire/Backend/TaxonomyTermTable.php new file mode 100644 index 00000000..871dbdc0 --- /dev/null +++ b/app/Http/Livewire/Backend/TaxonomyTermTable.php @@ -0,0 +1,58 @@ +taxonomy = $taxonomy; + } + + public function columns(): array + { + return [ + Column::make("Code", "code") + ->searchable()->sortable(), + Column::make("Name", "name") + ->searchable()->sortable(), + Column::make("Taxonomy", "taxonomy.name") + ->searchable() + ->sortable(), + Column::make("Created by", "created_by") + ->sortable(), + Column::make("Updated by", "updated_by") + ->sortable(), + Column::make("Created at", "created_at") + ->sortable(), + Column::make("Updated at", "updated_at") + ->sortable(), + Column::make("Actions") + ]; + } + + public function query(): Builder + { + return TaxonomyTerm::query() + ->where('taxonomy_id', $this->taxonomy->id) + ->with('user'); + } + + public function rowView(): string + { + return 'backend.taxonomy.terms.index-table-row'; + } +} diff --git a/resources/views/backend/taxonomy/index-table-row.blade.php b/resources/views/backend/taxonomy/index-table-row.blade.php index b5b65e83..19174a30 100644 --- a/resources/views/backend/taxonomy/index-table-row.blade.php +++ b/resources/views/backend/taxonomy/index-table-row.blade.php @@ -29,22 +29,22 @@
Are you sure you want to delete + "{{ $term->name }}" ? +
+