Alexander Kim Alexander Kim - 20 days ago 5
PHP Question

Laravel update method not working correctly

Here's my controller code:

public function update(Request $request, $id)
{
$currency = Currency::find($id);

$this->validate($request, [
'cur_name' => 'required',
'cur_price' => 'required|numeric',
'cur_icon' => 'required|image|max:100',
'cur_reserve' => 'required|numeric',
]);

$currency->cur_name = strip_tags($request->input('cur_name'));
$currency->cur_price = $request->input('cur_price');

// image
$file = $request->file('cur_icon');

if (isset($file)) {
$filename = Str::lower(
pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '-' . uniqid() . '.' . $file->getClientOriginalExtension()
);
$destination = 'uploads/';
$file->move($destination, $filename);
$currency->cur_icon = $filename;
}

$currency->cur_reserve = $request->input('cur_reserve');
$currency->slug = \Slug::make(strip_tags($request->input('cur_name')));
dd($currency);
$currency->save();

return redirect('/admin/currency');
}


edit.blade.php (short):

<form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="/admin/currency/{{ $currency->id }}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
...
</form>


routes:

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Route::resource('currency', 'CurrencyController');
});


When i click on submit - i am not being redirected and nothing happens, just page reloads - that's all. Even if i modify data no changes are saved to database.

Answer

If you aren't getting to dd($currency); before $currency->save()? then it may be the validator redirecting. Try debugging with this in your view to check:

{{ count($errors) > 0 ? dd($errors->all()) : ''}}
Comments