-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminProductController.php
91 lines (78 loc) · 2.4 KB
/
AdminProductController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class AdminProductController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$products = Product::all();
return view('admin.products.index', compact('products'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$product = new Product();
return view('admin.products.create', ['product' => $product]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validatedData = $this->validatedData($request);
Product::create($validatedData);
return redirect()->route('admin.products.index')->with('success', 'Product created successfully!');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$product = Product::findOrFail($id);
return view('admin.products.show', compact('product'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$product = Product::findOrFail($id);
return view('admin.products.edit', compact('product'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$product = Product::findOrFail($id);
$validatedData = $this->validatedData($request, $product);
$product->update($validatedData);
return redirect()->route('admin.products.index')->with('success', 'Product updated successfully!');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$product = Product::find($id);
$product->delete();
return redirect()->route('admin.products.index')->with('success', 'Product deleted successfully!');
}
private function validatedData(Request $request, $product = null)
{
$rules = [
'name' => 'required|max:255',
'price' => 'required|numeric',
'description' => 'nullable',
'item_number' => 'required|unique:products,item_number' . ($product ? ',' . $product->id : ''),
'image' => 'nullable|url'
];
return $request->validate($rules);
}
}