提交表单按钮不起作用laravel 5.7

时间:2018-12-09 21:20:06

标签: php laravel

我有两种情况,要求用户更新calls数据库上的记录。一种是将call分配给工程师(我已经成功实现了)。第二个是call要关闭时。我有两个控制器;一个处理第一个过程,第二个closeCallsController处理第二个过程。

我正尝试使用此表单

更新calls数据库中的记录
<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>

但是,当我单击Submit按钮时,它只是重新加载页面,而不更新数据库或重定向到closedCalls页面。

这是我的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');
}

这是我的路线

Route::get('/pages/closeCall/{id}', 'CloseCallsController@edit');
Route::resource('CloseCalls', 'CloseCallsController');

这是我的Call模型

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');
}   

请问我该如何解决?

2 个答案:

答案 0 :(得分:4)

您没有在更新表单操作中指定的名为“呼叫路由”的路由。

尝试一下

<form action="{{route('CloseCalls.update', $calls->id)}}" method="POST"></form>

答案 1 :(得分:1)

我更新了路线,就像@Jasim正确说的一样,但表单仍未提交。经过一些测试,我发现代码在验证过程中中断,因此,我添加了

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

为了显示错误,立即在@csrf之前,然后我发现表单中未包括字段pending_on,但在验证检查中将其设置为required 。所以,我只是删除了它,代码就起作用了...谢谢大家