Reputation: 227
working with Laravel 5.6 and MySQL. I am going to update categoryname and image in my categories table using the following controller function?
public function update(Request $request, $id)
{
if ($request->isMethod('get'))
return view('categories.form', ['image' => Category::find($id)]);
else {
$rules = [
'categoryname' => 'required',
];
$this->validate($request, $rules);
$image = Category::find($id);
if ($request->hasFile('image')) {
$dir = 'images/';
if ($image->image != '' && File::exists($dir . $image->image))
File::delete($dir . $image->image);
$extension = strtolower($request->file('image')->getClientOriginalExtension());
$fileName = str_random() . '.' . $extension;
$request->file('image')->move($dir, $fileName);
$image->categoryimage = $fileName;
} elseif ($request->remove == 1 && File::exists('images/' . $image->image)) {
File::delete('images/' . $image->post_image);
$image->categoryimage = null;
}
}
$image->categoryname = $request->categoryname;
$image->save();
return redirect()->route('categories.index');
}
and route
Route::match(['get', 'put'], 'category/update/{id}', 'CategoryController@update');
and edit form
@if(isset($image))
<form method="PUT" action="http://localhost:8000/category/update/{{$image->id}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="put">
<label for="description" class="col-form-label col-md-3 col-lg-2">Description</label>
<div class="col-md-8">
<input class="form-control" autofocus placeholder="Description" name="categoryname" type="text" id="categoryname" value="{{ isset($image) ? $image->categoryname : '' }}">
<label for="image" class="col-form-label col-md-3">Image</label>
<div class="col-md-5">
<img id="preview"
src="{{asset((isset($image) && $image->categoryimage!='')?'images/'.$image->categoryimage:'images/noimage.png')}}"
height="200px" width="200px"/>
<input class="form-control" style="display:none" name="image" type="file" id="image" name="_token" value="{{ csrf_token() }}">
<br/>
<a href="javascript:changeProfile();">Add Image</a> |
<a style="color: red" href="javascript:removeImage()">Remove</a>
<input type="hidden" style="display: none" value="0" name="remove" id="remove">
but when I try to update data it is not updating. only refresh to the same page. no, any error
url look like this
http://localhost:8000/category/update/21?categoryname=tractorrer&image=&remove=0 //tractorrer is updated category name
how can fix this problem?
Upvotes: 0
Views: 73
Reputation: 2126
Have you dump all of the request like this to check whether it contains categoryname
.
dd($request->all());
If the dd
does not print categoryname
value, then it seems to be the problem when you sending request.
Try using method="POST"
instead of method="PUT"
If you have not try yet.
I have never used PUT
with form submit before
Upvotes: 1
Reputation: 1050
First,
You set POST instead of PUT of form method.
Second,
Your codes look a little messy. I updated your action codes;
public function update(Request $request, $id)
{
if ($request->isMethod('get')){
return view('categories.form', ['image' => Category::find($id)]);
} else{
$rules = [
'categoryname' => 'required',
];
$this->validate($request, $rules);
$image = Category::find($id);
if ($request->hasFile('image')) {
$dir = 'images/';
if ($image->image != '' && File::exists($dir . $image->image))
File::delete($dir . $image->image);
$extension = strtolower($request->file('image')->getClientOriginalExtension());
$fileName = str_random() . '.' . $extension;
$request->file('image')->move($dir, $fileName);
$image->categoryimage = $fileName;
} elseif ($request->remove == 1 && File::exists('images/' . $image->image))
{
File::delete('images/' . $image->post_image);
$image->categoryimage = null;
}
$image->categoryname = $request->categoryname;
$image->save();
return redirect()->route('categories.index');
}
}
Can you try again with my directives. I hope it helping your problem.
Upvotes: 0
Reputation: 2008
Theres a few things concerning about your code examples. Like only having 1 method called update
for both GET and PUT routes. Probably not a good habit to get into. Especially since, if it is a get route, you're not updating anything. So already the method name / description of what it does, is wrong.
But that aside, you are not seeing anything because you cannot use method="PUT"
on your form. You need to use method="POST"
and then inside your form, you need these lines. One to tell the form that this is a patch request, and one to put the csrf token in.
{{csrf_field()}}
{{ method_field('PATCH') }}
and I would update your route to PATCH, not PUT.
Upvotes: 1