-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductControllerOLD.php
135 lines (123 loc) · 3.84 KB
/
ProductControllerOLD.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\ProductImage;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
return Product::with('Images')->where('status', '=', 'published')->paginate($request->input('limit', 10));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$product = new Product();
$request->validate([
'title' => 'required',
'description' => 'required',
'price' => 'required | numeric',
'discount' => 'required | numeric',
'About' => 'required'
]);
$productCreated = $product->create([
'category' => $request->category,
'title' => $request->title,
'description' => $request->description,
'price' => $request->price,
'About' => $request->About,
'discount' => $request->discount,
]);
return $productCreated;
}
/**
* Display the specified resource.
*/
public function show($id)
{
return Product::where('id', $id)->with('Images')->where('status', '=', 'published')->get();
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Product $product)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
$product = Product::findOrFail($id);
$request->validate([
'category' => 'required',
'title' => 'required',
'description' => 'required',
'price' => 'required | numeric',
'discount' => 'required | numeric',
'About' => 'required'
]);
$product->update([
'category' => $request->category,
'title' => $request->title,
'description' => $request->description,
'price' => $request->price,
'About' => $request->About,
'discount' => $request->discount,
]);
$product->status = 'published';
$product->save();
$productId = $product->id;
if ($request->hasFile('images')) {
$files = $request->file("images");
$i = 0;
foreach ($files as $file) {
$i = $i + 1;
$image = new ProductImage();
$image->product_id = $productId;
$filename = date('YmdHis') . $i . '.' . $file->getClientOriginalExtension();
$path = 'images';
$file->move($path, $filename);
$image->image = url('/') . '/images/' . $filename;
$image->save();
}
}
}
// Search On Users
public function search(Request $request)
{
$query = $request->input('title');
$results = Product::with('images')->where('title', 'like', "%$query%")->get();
return response()->json($results);
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$productImages = ProductImage::where('product_id', '=', $id)->get();
foreach ($productImages as $productImage) {
$path = public_path() . '/images/' . substr($productImage['image'], strrpos($productImage['image'], '/') + 1);
if (File::exists($path)) {
File::delete($path);
}
}
DB::table('products')->where('id', '=', $id)->delete();
}
}