-
Notifications
You must be signed in to change notification settings - Fork 0
/
Untitled Document 1
156 lines (99 loc) · 3.59 KB
/
Untitled Document 1
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->description = $request->description;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '_' . $image->getClientOriginalName();
$path = $image->storeAs('public/images', $filename);
$post->image = 'storage/images/' . $filename;
}
$post->save();
return redirect()->route('posts.index');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required',
'content' => 'required',
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048'
]);
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('images/' . $filename);
Image::make($image->getRealPath())->resize(300, 300)->save($path);
$post->image = 'images/' . $filename;
}
$post->save();
return redirect()->route('your_route.index')
->with('success', 'Post created successfully');
}
public function store(Request $request)
{
// Validate request data
$validated = $request->validate([
'name' => 'required|max:255',
'description' => 'required',
'price' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
// Store image
$image = $request->file('image');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $imageName);
// Create new product
$product = new Product;
$product->name = $validated['name'];
$product->description = $validated['description'];
$product->price = $validated['price'];
$product->image = $imageName;
$product->save();
return redirect()->route('products.index')
->with('success','Product created successfully.');
}
public function show(Product $product)
{
return view('products.show', compact('product'));
}
<img src="{{ asset('images/'.$product->image) }}" alt="{{ $product->name }}" width="200">
public function update(Request $request, Product $product)
{
// Validate request data
$validated = $request->validate([
'name' => 'required|max:255',
'description' => 'required',
'price' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('image')) {
// Store new image
$image = $request->file('image');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $imageName);
// Delete old image
File::delete(public_path('images/'.$product->image));
// Update product's image
$product->image = $imageName;
}
// Update other product data
$product->name = $validated['name'];
$product->description = $validated['description'];
$product->price = $validated['price'];
$product->save();
return redirect()->route('products.index')
->with('success','Product updated successfully.');
}
public function destroy(Product $product)
{
// Delete product's image
File::delete(public_path('images/'.$product->image));
// Delete product
$product->delete();
return redirect()->route('products.index')
->with('success','Product deleted successfully.');
}