You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB
PHTML

3 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
3 years ago
use Illuminate\Validation\Rule;
3 years ago
class CategoryController extends Controller
{
public function index()
{
return inertia('Category', [
3 years ago
'categories' => Category::orderBy('created_at', 'asc')->paginate(10)
3 years ago
]);
}
public function store(Request $request)
{
$request->validate([
3 years ago
'name' => [
'required',
'string',
'max:255',
Rule::unique('categories', 'name')->where(function ($query) {
return $query->where('deleted_at', null);
})
],
3 years ago
'description' => 'required|string|max:255',
'amount' => 'required|numeric|max:999999999|min:1'
]);
$category = Category::create([
'name' => $request->name,
'description' => $request->description,
'default_budget' => $request->amount
]);
$category->budgets()->create([
'budget' => $request->amount,
'start_date' => now()->toDateString(),
3 years ago
'end_date' => null,
3 years ago
'remain' => $request->amount
3 years ago
]);
return redirect()->route('categories');
}
public function update(Request $request, Category $category)
{
$request->validate([
'name' => 'required|string|max:255',
'description' => 'required|string|max:255',
'amount' => 'required|numeric|max:999999999|min:1'
]);
$category->update([
'name' => $request->name,
'description' => $request->description,
3 years ago
'default_budget' => $request->amount,
3 years ago
]);
3 years ago
$budget = $category->budgets()->where('end_date', null);
3 years ago
$budget->update(['budget' => $request->amount]);
return redirect()->route('categories');
}
public function destroy(Category $category)
{
$category->budgets()->delete();
$category->delete();
return redirect()->route('categories');
}
}