first commit
This commit is contained in:
54
app/Http/Controllers/CategoryController.php
Normal file
54
app/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
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', '分類已刪除');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user