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.

57 lines
1.4 KiB
PHTML

2 years ago
<?php
namespace App\Http\Controllers;
2 years ago
use App\Models\Category;
2 years ago
use Illuminate\Http\Request;
class CategoryController extends Controller
{
2 years ago
public function index(Request $request)
{
$query = Category::query();
if ($request->q) {
2 years ago
$query->where('name', 'like', "%{$request->q}%");
2 years ago
}
$query->orderBy('created_at', 'desc');
return inertia('Category/Index', [
'query' => $query->paginate(10),
]);
}
public function store(Request $request)
{
$request->validate([
'name' => 'string|required|max:255',
]);
Category::create(['name' => $request->name]);
return redirect()->route('category.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed saved']);
}
public function update(Request $request, Category $category)
{
$request->validate([
'name' => 'string|required|max:255'
]);
$category->update(['name' => $request->name]);
return redirect()->route('category.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed updated']);
}
public function destroy(Category $category)
{
$category->delete();
return redirect()->route('category.index')
->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']);
}
2 years ago
}