您好,我正在尝试更新表格助手的寄存器,但是当我按下表单的提交按钮时,会出现此错误,我在助手和事件之间使用了表格枢轴,但是我只是尝试编辑助手。This is the error
这是我的代码AssistantController.php文件以及方法的编辑和更新
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
这是我给助理的表格 input form assistants
@csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
这是我的编辑页面
@extends('layouts.back')
@section('title','Edit assistants')
@section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
@method('PATCH')
@include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
<a href="{{@route('assistants.index',[id=>$event[0]])}}" class="btn btn-danger">Cancel</a>
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
@endsection
有我的路线 routes
Auth::routes();
Route::get('/', 'HomeController@index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
我是初学者。
谢谢大家。
答案 0 :(得分:0)
使用Route::resource
时,laravel会根据REST标准自动搭建指向控制器的路由
如果您运行php artisan route:list
,将会得到类似的内容
+-----------+-----------------------------+--------------------+--------------------------------------------------+
| Method | URI | Name | Action |
+-----------+-----------------------------+--------------------+--------------------------------------------------+
| GET|HEAD | assistants | assistants.index | App\Http\Controllers\AssistantController@index |
| POST | assistants | assistants.store | App\Http\Controllers\AssistantController@store |
| GET|HEAD | assistants/create | assistants.create | App\Http\Controllers\AssistantController@create |
| GET|HEAD | assistants/{assistant} | assistants.show | App\Http\Controllers\AssistantController@show |
| PUT|PATCH | assistants/{assistant} | assistants.update | App\Http\Controllers\AssistantController@update |
| DELETE | assistants/{assistant} | assistants.destroy | App\Http\Controllers\AssistantController@destroy |
| GET|HEAD | assistants/{assistant}/edit | assistants.edit | App\Http\Controllers\AssistantController@edit |
+-----------+-----------------------------+--------------------+--------------------------------------------------+
因此,在您的表单中,您必须传递模型ID(assistant-> id)或为路由模型绑定设置的任何字段
<form action="{{ route('assistants.update', ['assistant' => $assistant->id]) }}" method="post">
@method('PATCH')
请注意,在本示例中,为了清楚起见,我们使用了路由名称
发生错误是因为表单操作
assisants
中的路径是模棱两可的,并且laravel假设第一个定义为索引,并且它期望使用GET或HEAD方法,或者期望第二个指向store
需要POST方法
答案 1 :(得分:0)
在您的编辑页面中,从
更改您的表单操作<form action="/assistants" method="POST">
到
<form action="/assistants/{{ $assistant->id }}" method="POST">
如果 action 字段中没有助手 id,Laravel 会认为您正在尝试更新基本助手路由,这是该路由的无效操作。