@@ -15,7 +15,7 @@ class ProductController extends Controller
15
15
*/
16
16
public function index ()
17
17
{
18
- $ products = Product::latest ()->get ();
18
+ $ products = Product::with ( ' category ' )-> latest ()->get ();
19
19
20
20
return response ()->json ($ products , 200 );
21
21
}
@@ -42,18 +42,20 @@ public function store(Request $request)
42
42
'title ' => 'required|max:255|unique:products,title ' ,
43
43
'price ' => 'required|integer ' ,
44
44
'image ' => 'required|image|max:2048 ' ,
45
- 'description ' => 'required '
45
+ 'description ' => 'required ' ,
46
+ 'category_id ' => 'required ' ,
46
47
]);
47
48
48
49
$ product = Product::create ([
49
50
'title ' => $ request ->title ,
50
51
'slug ' => Str::slug ($ request ->title ),
51
52
'price ' => $ request ->price ,
52
53
'description ' => $ request ->description ,
54
+ 'category_id ' => $ request ->category_id ,
53
55
]);
54
56
55
- if ($ request ->image ){
56
- $ imageName = time (). '_ ' . uniqid () .'. ' . $ request ->image ->getClientOriginalExtension ();
57
+ if ($ request ->image ) {
58
+ $ imageName = time () . '_ ' . uniqid () . '. ' . $ request ->image ->getClientOriginalExtension ();
57
59
$ request ->image ->move (public_path ('storage/product ' ), $ imageName );
58
60
$ product ->image = '/storage/product/ ' . $ imageName ;
59
61
$ product ->save ();
@@ -92,23 +94,25 @@ public function edit(Product $product)
92
94
* @return \Illuminate\Http\Response
93
95
*/
94
96
public function update (Request $ request , Product $ product )
95
- {
97
+ {
96
98
$ this ->validate ($ request , [
97
99
'title ' => "required|max:255|unique:products,title, $ product ->id " ,
98
100
'price ' => 'required|integer ' ,
99
101
'image ' => 'sometimes|nullable|image|max:2048 ' ,
100
- 'description ' => 'required '
102
+ 'description ' => 'required ' ,
103
+ 'category_id ' => 'required ' ,
101
104
]);
102
105
103
106
$ product ->update ([
104
107
'title ' => $ request ->title ,
105
108
'slug ' => Str::slug ($ request ->title ),
106
109
'price ' => $ request ->price ,
107
110
'description ' => $ request ->description ,
111
+ 'category_id ' => $ request ->category_id ,
108
112
]);
109
113
110
- if ($ request ->image ){
111
- $ imageName = time (). '_ ' . uniqid () .'. ' . $ request ->image ->getClientOriginalExtension ();
114
+ if ($ request ->image ) {
115
+ $ imageName = time () . '_ ' . uniqid () . '. ' . $ request ->image ->getClientOriginalExtension ();
112
116
$ request ->image ->move (public_path ('storage/product ' ), $ imageName );
113
117
$ product ->image = '/storage/product/ ' . $ imageName ;
114
118
$ product ->save ();
@@ -125,16 +129,16 @@ public function update(Request $request, Product $product)
125
129
*/
126
130
public function destroy (Product $ product )
127
131
{
128
- if ($ product ){
132
+ if ($ product ) {
129
133
$ productImage = $ product ->image ;
130
134
$ imagePath = public_path ($ productImage );
131
-
132
- if ($ productImage && file_exists ($ imagePath )){
135
+
136
+ if ($ productImage && file_exists ($ imagePath )) {
133
137
unlink ($ imagePath );
134
138
}
135
139
136
140
$ product ->delete ();
137
- }else {
141
+ } else {
138
142
return response ()->json ('Product not found. ' , 404 );
139
143
}
140
144
}
0 commit comments