Reputation: 424
I have two scenarios where a user is required to update the record on the calls
database. One is when the call
is to be assigned to an engineer (this I have successfully implemented). The second is when a call
is to be closed. I have two controllers; one handling the first process and the second closeCallsController
handling this second process.
Am trying to update a record in the calls
database with this form
<form action="{{route('Call.update', $calls->id)}}" method="POST">
@csrf
{{method_field('PATCH')}}
<div class="col-md-6">
<div class="input-group" style="width: 100%;">
<label for="terminal_id">{{ __('Terminal ID') }}</label><br>
<input type="text" name="terminal_id" id="terminal_id" class="form-control" value="{{$calls->terminal_id}}" style="padding: 20px;" readonly>
</div>
</div>
<div class="col-md-6">
<div class="input-group col-md-12" style="width: 100%;">
<label for="terminal_name">{{ __('Terminal name') }}</label><br>
<input type="text" name="terminal_name" id="terminal_name" class="form-control" value="{{$calls->terminal_name}}" style="padding: 20px;" readonly>
</div>
</div>
<div class="col-md-6">
<div class="input-group" style="width: 100%;">
<label for="closed_on">{{ __('Date and time of closure') }}</label><br>
<input type="datetime-local" name="closed_on" id="closed_on" class="form-control" style="padding: 20px;" required>
</div>
</div>
<div class="input-group col-xs-6 form-group">
<label for="operations_comment">{{ __('Comment') }}</label><br>
<textarea name="operations_comment" id="operations_comment" class="form-control" rows="5" cols="20" required></textarea>
</div>
<button type="submit" class="btn-primary" style="padding: 10px; font-size: 16px; border: 0;">{{ __('Submit') }}</button>
</form>
But when I click on the Submit
button, it simply reloads the page without updating the database nor redirecting to the closedCalls
page.
here is my closeCallsContoller
public function edit($id)
{
$calls = Call::find($id);
return view('pages.closeCall')->with('calls', $calls);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//validate
$this->validate($request,[
'terminal_id' => 'required',
'terminal_name' => 'required',
'pending_on' => 'required',
'closed_on' => 'required',
'operations_comment' => 'required'
]);
//store in the database
$call = Call::find($id);
$call->terminal_id = $request->input('terminal_id');
$call->terminal_name = $request->input('terminal_name');
$call->closed_on = $request->input('closed_on');
$call->operations_comment = $request->input('operations_comment');
$call->call_status = 'Closed';
$call->pending_on = 'Closed';
$call->closed_by = Auth::user()->name;
$call->save();
return redirect('/pages/closedCalls');
}
And this is my route
Route::get('/pages/closeCall/{id}', 'CloseCallsController@edit');
Route::resource('CloseCalls', 'CloseCallsController');
This is my Call
model
class Call extends Model{
protected $fillable = [
'terminal_id',
'terminal_name',
'call_status',
'pending_on',
'closed_on',
'operations_comment',
'closed_by'
];
//describing a one-to-many-relationship between calls and users
public function user(){
return $this->belongsTo('App\User');
}
Please how can I fix this?
Upvotes: 0
Views: 5577
Reputation: 424
I updated my route just like @Jasim rightly said but the form still didn't submit. After some tests, I discovered that the code breaks during validation so, I added
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Immediately before @csrf
in order to display the errors, then I found out that the field pending_on
was not included on the form, but it was set to required
on the validation check. so, I simply deleted it and the code works...thanks guys
Upvotes: 1
Reputation:
You don't have any route named Call route which you give in the update form action.
Try with this
<form action="{{route('CloseCalls.update', $calls->id)}}" method="POST"></form>
Upvotes: 4