如何解决当我点击laravel 8中的提交按钮时,提交表单连接两个网址
你好,我是 laravel 的新手,问题是我创建了两个函数,一个用于查看编辑表单,另一个用于更新数据,但是当我点击提交时,concat 视图并更新两个 url 但在表单操作中我有只给了它更新网址。
Route::post('addStudent',[StudentController::class,'addStudent'])->name('addStudent');
Route::get('/president',[PresidentController::class,'index']);
Route::get('editPresident/{id}','presidentEditForm']);
Route::post('updatePresident','updatePresident'])->name('updatePresident');
这是我的控制器代码
// view president edit from
function presidentEditForm($id)
{
$userData=$this->user->getPresidentById($id);
return view('president/edit_president',['president'=>$userData]);
}
// update president from the admin
function updatePresident(Request $data)
{
$validated = $data->validate([
'first_name' => 'required|max:255','last_name' => 'required|max:255','email' => 'required|max:255','mob_no'=>'required','city'=>'required','join_date'=>'required'
]);
// saving inputs into variables
$firstname=$data->first_name;
$lastname=$data->last_name;
$email=$data->email;
$mob_no=$data->mob_no;
$city=$data->city;
$password=$data->password;
$joiningdate=$data->join_date;
$joiningtill=$data->join_till;
$userid=$data->userId;
// saving into the users object
$this->user->firstname=$firstname;
$this->user->lastname=$lastname;
$this->user->email=$email;
if($password!="")
{
$this->user->password=$password;
}
$this->user->joining_from=$joiningdate;
$this->user->joining_till=$joiningtill;
$this->user->city=$city;
$this->user->mobile_no=$mob_no;
}
这是我的视图代码
@extends('includes.header')
@section('content')
<x-pagetitle data="Edit President"/>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
<button type="button" onclick="back()" class="btn btn-dark btn-xs btn_back my-2"><i class="fas fa-arrow-left"></i> Go Back</button>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
<div class="card">
<div class="card-body">
<form action="updatePresident" method="POST">
@csrf
@foreach ($president as $p)
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
<label for="first_name">First name*</label>
<input type="text" class="form-control" id="first_name" placeholder="First name" name="first_name" value="{{$p['firstname']}}">
<input type="hidden" name="userId" value="{{$p['usr_id']}}">
<div class="invalid-Feedback">
@error('first_name')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 ">
<label for="last_name">Last name*</label>
<input type="text" class="form-control" id="last_name" placeholder="Last name" name="last_name" value="{{$p['lastname']}}">
<div class="invalid-Feedback">
@error('last_name')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="email">Email*</label>
<input type="text" class="form-control" id="email" placeholder="Email" name="email" value="{{$p['email']}}">
<div class="invalid-Feedback">
@error('email')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="password">Password*</label>
<input type="text" class="form-control" id="password" placeholder="password" name="password">
<div class="invalid-Feedback">
@error('password')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="mob_no">Phone No/Mobile No*</label>
<input type="text" class="form-control" id="mob_no" placeholder="Mobile/Phone No" name="mob_no" value="{{$p['mobile_no']}}">
<div class="invalid-Feedback">
@error('mob_no')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="city">City*</label>
<input type="text" class="form-control" id="city" placeholder="City" name="city" value="{{$p['city']}}">
<div class="invalid-Feedback">
@error('city')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="join_date">Joining Date*</label>
<div class="input-group date" id="join_date" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#join_date" name="join_date" value="{{$p['joining_from']}}" placeholder="Job Start Date">
<div class="input-group-append" data-target="#join_date" data-toggle="datetimepicker">
<div class="input-group-text"><i class="far fa-calendar-alt"></i></div>
</div>
</div>
<div class="invalid-Feedback">
@error('join_date')
{{ $message }}
@enderror
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12 mb-2">
<label for="join_till">Joining Till</label>
<div class="input-group date" id="join_till" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#join_till" name="join_till" value="{{$p['joining_till']}}" placeholder="Job Start Date">
<div class="input-group-append" data-target="#join_till" data-toggle="datetimepicker">
<div class="input-group-text"><i class="far fa-calendar-alt"></i></div>
</div>
</div>
<div class="invalid-Feedback">
@error('join_till')
{{ $message }}
@enderror
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 ">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</div>
@endforeach
</form>
</div>
</div>
</div>
</div>
@endsection
这是错误 enter image description here
解决方法
您需要在表单中添加@method('PUT') 然后您可以更新您的数据 你应该检查通过的路线
php artisan route:list
并检查那里的路线方法,您可以使用其他方法
@method('你的方法名') ,首先将 @method('put')
添加到您的表单中。
然后,将对象 id 发送到路由中,然后在验证后将更新函数更改为简单
$user->update($request->all());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。