我想更新注册表单数据。所以,起初我已经为show数据创建了一个Table。在这个表中,我已经包含了一个ViewForUpdate按钮,我已经通过了Id。当我单击ViewForUpdate按钮时,它会在另一个页面中正确显示唯一数据。输入新数据后,当我点击更新按钮时,显示此错误 -
MethodNotAllowedHttpException
那么,如何解决这个问题?
这是RegViewUpdate.blade.php文件
<html>
<head>
<body>
<form action="edit{{$users[0]->id}}" method="post" enctype="multipart/form-data">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
</div>
<div class="form-group">
<label>Upload Profile Picture :</label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Update">
</form>
</body>
</html>
这是RegViewController.php文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class RegViewController extends Controller
{
public function index()
{
return view('RegView');
}
public function show($id) {
$users = DB::select('select * from academic where id = ?',[$id]);
return view('RegViewUpdate',['users'=>$users]);
}
public function edit(Request $request, $id)
{
$name = $request->input('name');
DB::update('update academic set name = ? where id = ?',[$name,$id]);
echo "Record updated successfully.<br/>";
}
}
这是我创建的路线。
Route::get('edit/{id}','RegViewController@show');
Route::post('edit{id}','RegViewController@edit');
academic
表格结构。
答案 0 :(得分:2)
试试这个:
RegViewUpdate.blade.php
<html>
<head>
<body>
<form action="edit/{{$users[0]->id}}" method="post" enctype="multipart/form-data">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
</div>
<div class="form-group">
<label>Upload Profile Picture :</label>
<input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Update">
</form>
</body>
</html>
RegViewController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class RegViewController extends Controller
{
public function index()
{
return view('RegView');
}
public function show($id) {
$users = DB::select('select * from academic where id = ?',[$id]);
return view('RegViewUpdate',['users'=>$users]);
}
public function edit(Request $request, $id)
{
$name = $request->input('name');
try {
DB::table('academic')
->where('id', $id)
->update(['name' => $name]);
echo "Record updated successfully.<br/>";
} catch (\Exception $ex) {
dd($ex);
}
}
}
路线:
Route::get('edit/{id}','RegViewController@show');
Route::put('edit/{id}','RegViewController@edit');
答案 1 :(得分:1)
我相信你会想删除以下一行:
{{ method_field('PUT') }}
原因是您的Route设置为使用POST方法,因此未定义PUT Route。或者,您可以更改它以指定PUT方法(这是多余的,因为您已经指定了),或者使用带有相应路径的{{Form :: open()}}标记。
答案 2 :(得分:1)
it should be PUT method on route
Form action should be -
/edit/{{$users[0]->id}}
and route should be -
Route::put('/edit/{id}','RegViewController@edit');
Because you are using PUT method to send id.
答案 3 :(得分:1)
我发现你的代码存在两个问题
1)在视图文件中替换您的表单标记,如下所示
{!!
Form::open(
['method' => 'PUT',
'route' => ['update','id of data to update']
])
!!}
// place your form content here
{!! Form::close() !!}
2)改变你的路线如下
路线:: PUT( '更新/ {ID}', 'CONTROLLER_NAME @ update_method_name');