Skip to content

Commit 06d018b

Browse files
committed
category_id column added to products table
category relation to product model category id added when creating and editing a product
1 parent ed5c478 commit 06d018b

File tree

7 files changed

+277
-19
lines changed

7 files changed

+277
-19
lines changed

app/Http/Controllers/ProductController.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ProductController extends Controller
1515
*/
1616
public function index()
1717
{
18-
$products = Product::latest()->get();
18+
$products = Product::with('category')->latest()->get();
1919

2020
return response()->json($products, 200);
2121
}
@@ -42,18 +42,20 @@ public function store(Request $request)
4242
'title' => 'required|max:255|unique:products,title',
4343
'price' => 'required|integer',
4444
'image' => 'required|image|max:2048',
45-
'description' => 'required'
45+
'description' => 'required',
46+
'category_id' => 'required',
4647
]);
4748

4849
$product = Product::create([
4950
'title' => $request->title,
5051
'slug' => Str::slug($request->title),
5152
'price' => $request->price,
5253
'description' => $request->description,
54+
'category_id' => $request->category_id,
5355
]);
5456

55-
if($request->image){
56-
$imageName = time().'_'. uniqid() .'.'.$request->image->getClientOriginalExtension();
57+
if ($request->image) {
58+
$imageName = time() . '_' . uniqid() . '.' . $request->image->getClientOriginalExtension();
5759
$request->image->move(public_path('storage/product'), $imageName);
5860
$product->image = '/storage/product/' . $imageName;
5961
$product->save();
@@ -92,23 +94,25 @@ public function edit(Product $product)
9294
* @return \Illuminate\Http\Response
9395
*/
9496
public function update(Request $request, Product $product)
95-
{
97+
{
9698
$this->validate($request, [
9799
'title' => "required|max:255|unique:products,title, $product->id",
98100
'price' => 'required|integer',
99101
'image' => 'sometimes|nullable|image|max:2048',
100-
'description' => 'required'
102+
'description' => 'required',
103+
'category_id' => 'required',
101104
]);
102105

103106
$product->update([
104107
'title' => $request->title,
105108
'slug' => Str::slug($request->title),
106109
'price' => $request->price,
107110
'description' => $request->description,
111+
'category_id' => $request->category_id,
108112
]);
109113

110-
if($request->image){
111-
$imageName = time().'_'. uniqid() .'.'.$request->image->getClientOriginalExtension();
114+
if ($request->image) {
115+
$imageName = time() . '_' . uniqid() . '.' . $request->image->getClientOriginalExtension();
112116
$request->image->move(public_path('storage/product'), $imageName);
113117
$product->image = '/storage/product/' . $imageName;
114118
$product->save();
@@ -125,16 +129,16 @@ public function update(Request $request, Product $product)
125129
*/
126130
public function destroy(Product $product)
127131
{
128-
if($product){
132+
if ($product) {
129133
$productImage = $product->image;
130134
$imagePath = public_path($productImage);
131-
132-
if($productImage && file_exists($imagePath)){
135+
136+
if ($productImage && file_exists($imagePath)) {
133137
unlink($imagePath);
134138
}
135139

136140
$product->delete();
137-
}else {
141+
} else {
138142
return response()->json('Product not found.', 404);
139143
}
140144
}

app/Product.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
class Product extends Model
88
{
99
protected $guarded = [];
10+
11+
public function category()
12+
{
13+
return $this->belongsTo(Category::class);
14+
}
1015
}

database/migrations/2020_07_28_033734_create_products_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function up()
2020
$table->integer('price');
2121
$table->string('image')->nullable();
2222
$table->text('description')->nullable();
23+
$table->unsignedBigInteger('category_id');
2324
$table->timestamps();
25+
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
2426
});
2527
}
2628

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy