2025-12-30 15:03:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
namespace App\Modules\Inventory\Controllers;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Modules\Inventory\Models\Category;
|
2025-12-30 15:03:19 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
class CategoryController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'name' => 'required|string|max:255|unique:categories,name',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Category::create([
|
|
|
|
|
'name' => $validated['name'],
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '分類已建立');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*/
|
|
|
|
|
public function update(Request $request, Category $category)
|
|
|
|
|
{
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'name' => 'required|string|max:255|unique:categories,name,' . $category->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$category->update($validated);
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '分類已更新');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(Category $category)
|
|
|
|
|
{
|
|
|
|
|
if ($category->products()->count() > 0) {
|
|
|
|
|
return redirect()->back()->with('error', '該分類下尚有商品,無法刪除');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$category->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '分類已刪除');
|
|
|
|
|
}
|
|
|
|
|
}
|