我一直在使用ajax post使用Laravel 5.2成功更新我的数据库信息。视图,控制器和模型到位,但是当我使用Laravel 5.7实现它时,出现此错误
Symfony \组件\ HttpKernel \异常\> MethodNotAllowedHttpException没有消息。
使用Laravel 5.7是否需要进行任何更改或调整?请帮忙。谢谢
查看:
<form class="update-member-form" id="update-member-form" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="text" name="id" class="form-control" id="primary_id" value="">
<div class="form-group">
<label for="recipient-name" class="col-form-label">Member ID:</label>
<input type="text" name="member_id" class="form-control" id="member_id" value="" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">First Name:</label>
<input type="text" name="fname" class="form-control" id="editMember_name" value="" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Middle Name:</label>
<input type="text" name="mname" class="form-control" id="editMember_mname" value="" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Last Name:</label>
<input type="text" name="lname" class="form-control" id="editMember_lname" value="" required>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" onClick="window.location.reload()" >Close</button>
<button type="button" class="btn btn-primary btn-update-member">Save updates</button>
</div>
JavaScript
$(".btn-update-member").click(function(e){
e.preventDefault();
$.post("{{ url('/updatemember') }}", $("#update-member-form").serialize(), function(data){
if(data.notify == "Success"){
swal({
title: "Record successfully updated",
text: "Message will close in 2 seconds",
type: "success",
timer: 2000
});
} else{
console.log(data.notify);
}
},"json");
});
web.php
Route::post('/updatemember', 'MembersController@update');
控制器
public function update(Request $request, $id)
{
//
$updateMember = Member::where( 'id', $request['id'] )
->update( $request->all() );
if( $updateMember ){
$notification = "Success";
} else{
$notification = "Failed";
}
return json_encode( array( 'notify'=>$notification ) );
}
答案 0 :(得分:0)
您的路线错误,因为您在更新函数中传递了$id
作为参数,因此也请更新视图文件
Route::post('/updatemember', 'MembersController@update');
更改为
Route::post('/updatemember/{id}', 'MembersController@update');
答案 1 :(得分:0)
如果您已经放入参数,则从方法中删除ID:
public function update(Request $request)
{
$updateMember = Member::where( 'id', $request['id'] )
->update( $request->all() );
if( $updateMember ){
$notification = "Success";
} else{
$notification = "Failed";
}
return json_encode( array( 'notify'=>$notification ) );
}
OR
您必须将路线更改为:
Route::post('/updatemember/{id}', 'MembersController@update');
以及在js中
$.post("{{ url('/updatemember/yourid') }}", $("#update-member-form").serialize(), function(data){
if(data.notify == "Success"){
...
} else{
console.log(data.notify);
}
编辑: 您必须在id输入中传递值为空的值:
<input type="text" name="id" class="form-control" id="primary_id" value="">
答案 2 :(得分:0)
问题是您正在使用POST,但实际上您必须执行PUT才能解决此问题
<input name="_method" type="hidden" value="PUT">
在表格之后,您只需粘贴
别忘了
<form method="post">
答案 3 :(得分:0)
好,让我们开始吧 Member.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
// if you changed the primary key id you need to set it here
protected $primaryKey = 'primary_id';
protected $guarded = [];
// Also to work it with route model binding you need this too
public function getRouteKeyName()
{
return 'primary_id';
}
}
然后是web.php
// Get The Form name param has to be the same name as the controller variable
Route::get('/members/{member}/edit', 'MembersController@edit');
// Patch the form.
// Not post because you are not creating.
// Not put because you are not replacing.
// Patch because you are updating
Route::patch('/members/{member}/edit', 'MembersController@update')->name('member.update');
然后,MembersController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Member;
class MembersController extends Controller
{
// Bind the route to the model. It throws 404 if the member does not exit;
public function edit(Request $request, Member $member)
{
return view('members.edit', compact('member'));
}
public function update(Request $request, Member $member)
{
die('i am here update');
}
}
当然还有表格
<form class="update-member-form" id="update-member-form" enctype="multipart/form-data">
@csrf
@method('PATCH')
<div class="form-group">
<label for="recipient-name" class="col-form-label">Member ID:</label>
<input type="text" name="member_id" class="form-control" id="member_id" value="{{$member->member_id}}" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">First Name:</label>
<input type="text" name="fname" class="form-control" id="editMember_name" value="{{$member->fname}}" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Middle Name:</label>
<input type="text" name="mname" class="form-control" id="editMember_mname" value="{{$member->mname}}" required>
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Last Name:</label>
<input type="text" name="lname" class="form-control" id="editMember_lname" value="{{$member->lname}}" required>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" onClick="window.location.reload()" >Close</button>
<button type="button" class="btn btn-primary btn-update-member">Save updates</button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
$(".btn-update-member").click(function(e){
e.preventDefault();
$.post("{{ route('member.update', $member->primary_id) }}", $("#update-member-form").serialize(), function(data){
if(data.notify == "Success"){
swal({
title: "Record successfully updated",
text: "Message will close in 2 seconds",
type: "success",
timer: 2000
});
} else{
console.log(data.notify);
}
},"json");
});
</script>
当然,您可以避免路由模型绑定,而从输入字段中获取ID或从路由中获取int ID,然后通过db查询成员是否退出并bla bla bla。
答案 4 :(得分:0)
这行代码对我有用。只需添加->except('_token')
Member::where( 'id', $request['id'])->update($request->except('_token'));