I have a form which updates a record however I am recieving the following error:
Method App\Handling::__toString() must not throw an exception, caught InvalidArgumentException: Data missing
The Handling
is my model.
This is my routes:
Route::get('/update-handling/{id}', 'HandlingController@edit');
Route::post('/update-handling/{id}', 'HandlingController@update')->name('postUpdateHandling');
The get
works fine and returns me the Handling
object correctly. When I try to update the form and go to post
route returns me the error above.
And this is my update
function:
public function update(Request $request, $id)
{
$name = $request->input('name');
$handling = $request->input('handling-thermic');
$thermic = 0;
$superficial = 0;
if ($handling == 0)
{
$thermic = 1;
}
else if ($handling == 1)
{
$superficial = 1;
}
$handling = Handling::find($id);
$handling->u_name = $name;
$handling->u_thermic = $thermic;
$handling->u_superficial = $superficial;
$handling->u_active = 1;
$handling->save();
}
My html form:
{!! Form::open(['id' => 'update-handling', 'url' => route('postUpdateHandling',['id' => $handling->id]), 'method' => 'post']) !!}
your form should be :
<form method="post" action="{{route('postUpdateHandling',['id'=>$handling->id])}}">
...
<button type="submit">Submit</button>
</form>
in your controller :
use Session;
public function update(Request $request, $id)
{
// first find record exists or not , if exits then update else redirect to 404 or home page
$handling = Handling::find($id);
if($handling){
$name = $request->input('name');
// added ternary condition for better syntax
$thermic = (int)$request->input('handling-thermic')==0 ? 1 : 0 ;
$superficial =(int)$request->input('handling-thermic')==1 ? 1 : 0 ;
$handling->u_name = $name;
$handling->u_thermic = $thermic;
$handling->u_superficial = $superficial;
$handling->u_active = 1;
$handling->save();
return redirect()->back()->with('success', 'Updated');
}else{
// redirect to 404 or homepage
return redirect('/home');
}
}
in your blade view add below snippet for display message :
@if (session()->has('success'))
<h1>{{ session('success') }}</h1>
@endif