Skip to content

Commit 9feff69

Browse files
author
Ivan
committed
Edit and Show are done
1 parent 55ef336 commit 9feff69

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

app/Http/Controllers/ProductController.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,40 @@ public function edit($id)
6969
}
7070
}
7171

72-
public function update(Request $request)
72+
public function update(Request $request, $id)
7373
{
74+
$validator = Validator::make($request->all(), [
75+
'name' => 'required|max:100',
76+
'price' => 'required|digits_between:0,99999',
77+
'description' => 'required|max:500',
78+
'picture' => 'mimes:jpeg,bmp,png,gif',
79+
]);
80+
81+
if ($validator->fails()) {
82+
return redirect()->back()->withErrors($validator);
83+
}
84+
85+
$product = Product::findOrFail($id);
86+
if ($product) {
87+
$product->name = $request->get('name');
88+
$product->price = $request->get('price');
89+
$product->description = $request->get('description');
90+
if ($request->get('picture')) {
91+
$picture = base64_encode(file_get_contents($request->get('picture')));
92+
$product->picture = $picture;
93+
}
94+
try {
95+
$product->save();
96+
return redirect("product/" . $product->id);
97+
} catch (Exception $e) {
98+
Log::error($e->getMessage());
99+
return redirect()->back()->withErrors(["errors" => array("Unable to update product. Please try again later")]);
100+
}
101+
} else {
102+
Log::error("Product with ID ($id) cannot be found to update.");
103+
abort(404, "Page not found");
104+
return false;
105+
}
74106

75107
}
76108

@@ -79,8 +111,10 @@ public function destroy($id)
79111
$product = Product::findOrFail($id);
80112
if ($product) {
81113
$product->delete();
114+
Log::notice("Product with ID ($id) is now deleted.");
82115
return redirect('/');
83116
} else {
117+
Log::error("Product with ID ($id) cannot be found to destroy.");
84118
abort(404, "Page not found");
85119
return false;
86120
}

resources/views/product/create.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<div class="row">
3939
<div class="col-sm-12 text-center">
4040
{!! Form::submit('Create Product', ["class"=>"btn btn-success"]) !!}
41+
<a href="{{url('/')}}" class="btn btn-warning">Cancel</a>
4142
</div>
4243
</div>
4344
{{ Form::close() }}

resources/views/product/edit.blade.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
{{-- create product form --}}
1919
<div class="row padding-top-bottom-10">
2020
<div class="col-sm-12">
21-
{{ Form::model($product, array('url' => '/product', 'method'=>'post', 'class' => 'form-horizontal', 'files' => true)) }}
21+
22+
{{-- {{ Form::model($product, array('url' => '/product', 'method'=>'post', 'class' => 'form-horizontal', 'files' => true)) }}--}}
23+
{{ Form::model($product, array('route' => array('product.update', $product->id), 'method' => 'patch', 'class'=>'form-horizontal', 'files' => true)) }}
2224
@if (count($errors) > 0)
2325
<div class="row">
2426
<div class="col-sm-6 col-sm-offset-3">
@@ -37,7 +39,8 @@
3739

3840
<div class="row">
3941
<div class="col-sm-12 text-center">
40-
{!! Form::submit('Create Product', ["class"=>"btn btn-success"]) !!}
42+
{!! Form::submit('Edit Product', ["class"=>"btn btn-success", "href"=>"#"]) !!}
43+
<a href="{{url('/')}}" class="btn btn-warning">Cancel</a>
4144
</div>
4245
</div>
4346
{{ Form::close() }}

resources/views/product/list.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<td>${{number_format($product['price'], 2, '.', ',')}}</td>
5151
<td align="center">
5252
<a class="btn btn-primary" href="{{url('/product/'. $product['id'] . '/edit')}}">Edit</a>
53-
{{ Form::open(array('route' => array('product.destroy', $product['id']), 'method' => 'delete', 'class'=>'inline-block')) }}
53+
{{ Form::open(array('route' => array('product.destroy', $product['id']), 'method' => 'delete', 'class'=>'inline-block', 'onsubmit'=>'return confirm("Do you want to delete this product?")')) }}
5454
{!! Form::submit('Delete', ["class"=>"btn btn-danger", "href"=>"#"]) !!}
5555
{{ Form::close() }}
5656
</td>

resources/views/product/show.blade.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,49 @@
66
* Time: 11:46 PM
77
*/
88
?>
9-
{!! $product !!}
9+
@extends('layout.basic')
10+
@section('title')
11+
Product - Detail Page
12+
@stop
13+
14+
@section('link')
15+
@stop
16+
17+
@section('content')
18+
{{-- create product form --}}
19+
<div class="row padding-top-bottom-10">
20+
<div class="col-sm-6 col-sm-offset-3">
21+
<img src="data:image/jpeg;base64,{!! $product->picture !!}" width="100%" alt="">
22+
23+
<table class="table table-bordered table-hover table-striped table-condensed">
24+
<tbody>
25+
<tr>
26+
<th>Product name</th>
27+
<td>{{$product->name}}</td>
28+
</tr>
29+
<tr>
30+
<th>Product price</th>
31+
<td>${{$product->price}}</td>
32+
</tr>
33+
<tr>
34+
<th>Product description</th>
35+
<td>{{$product->description}}</td>
36+
</tr>
37+
</tbody>
38+
</table>
39+
</div>
40+
</div>
41+
<div class="row">
42+
<div class="col-sm-6 col-sm-offset-3 text-center">
43+
<a href="{{url('/')}}" class="btn btn-primary">Show List</a>
44+
<a href="{{url('product/'. $product->id . '/edit')}}" class="btn btn-primary">Edit</a>
45+
{{ Form::open(array('route' => array('product.destroy', $product['id']), 'method' => 'delete', 'class'=>'inline-block')) }}
46+
{!! Form::submit('Delete', ["class"=>"btn btn-danger", "href"=>"#"]) !!}
47+
{{ Form::close() }}
48+
</div>
49+
</div>
50+
@stop
51+
52+
@section('script')
53+
54+
@stop

0 commit comments

Comments
 (0)