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.

68 lines
1.8 KiB
PHTML

3 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
class CategoryController extends Controller
{
public function index()
{
return inertia('Category', [
3 years ago
'categories' => Category::orderBy('created_at', 'desc')->paginate(10)
3 years ago
]);
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'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
]);
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,
'default_budget' => $request->amount
]);
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');
}
}