【问题标题】:How to do edit and update function in Laravel 8.9?如何在 Laravel 8.9 中进行编辑和更新功能?
【发布时间】:2021-04-04 22:52:24
【问题描述】:

提交注册表后,我想将其重定向到编辑/更新表单,但出现错误。编辑/更新表单是根据用户插入的注册表单从数据库中获取值。因此,每个用户都可以查看他们输入的表单并进行更新。

404 |未找到

编辑表单是homedit.blade.php。这是代码。

   <form method="post" action="{{route(homedit,$hm->id)}}"  enctype="multipart/form-data">
                @csrf
              
                    <label for="fullName"><b>Full Name as per NRIC :</b></label>
                    <input type="text" class="form-control" name="fullName" value="{{$hm['fullName']}}"/>
               
                    <label for="icno"><b>I/C No :</b></label>
                    <input type="tel" class="form-control" name="icno" pattern="[0-9]{12}" value="{{$hm['icno']}}"/>
                
                    <label for="email"><b>Email :</b></label>
                    <input type="text" class="form-control" name="email" value="{{Auth::user()->email}}"/>
               
                    <label for="gender"><b>Gender :</b></label>
                     <select name="gender" id="gender" class="form-control" >
                        @foreach ($hm as $home)
                        <option value="{{$hm['id']}}"> {{$hm['gender']}}</option>
                        @endforeach
                        <option value="female"> female</option>
                    </select>
                  </form>

这是控制器的代码

    public function createhome(Request $request)
     {
       return view('home');
     }

public function home(Request $request)
{
  
    $hm = new Register();
    $hm->fullName = $request->input('fullName');
    $hm->icno = $request->input('icno');
    $hm->email = $request->input('email');
    $hm->gender = $request->input('gender');
    $hm->record_id = Auth::user()->id;

    $hm->save();
    return redirect('/homedit')->with('success', 'Your personal details have been saved!');
}  

   public function updatehome(Request $request, $id)
{  
    $hm = Register::find($id);
    $hm->fullName = $request->fullName;
    $hm->icno = $request->icno;
    $hm->email = $request->email;
    $hm->gender = $request->gender;
  
    $hm->save();
    return redirect('/homedit')->with('success', 'Your personal details have been updated!');
}


public function edithome($id){
    $hm = Register::find($id);
    return view('homedit', compact ('hm'));

}

这是 web.php 的代码

Route::get('/home', 'App\Http\Controllers\ApplicantController@createhome');
Route::post('/home', 'App\Http\Controllers\ApplicantController@home');

Route::get('/homedit/{id}', 'App\Http\Controllers\ApplicantController@edithome');
Route::post('/homedit/{id}', 'App\Http\Controllers\ApplicantController@updatehome');

【问题讨论】:

  • 欢迎来到 SO ... 在重定向到该 URI 时,您需要为该路由 /homedit/{id}{id} 段传递一些东西

标签: php html laravel routes controller


【解决方案1】:

好的,这里的错误是你甚至没有设置路由名称,那么你是如何使用的 action="{{route(homedit,$hm-&gt;id)}}" 路由函数采用路由名称,而不是 uri。所以将您的路由更新为

Route::post('/homedit/{id}', 'App\Http\Controllers\ApplicantController@updatehome')->name('homedit');

最重要的是,尽可能多地遵循框架方向。对于记录更新,Laravel 使用“PUT”请求并使用隐式绑定。

【讨论】:

  • 你的意思是这样吗? Route::get('/homedit', 'App\Http\Controllers\ApplicantController@edithome')-&gt;name('homedit'); Route::post('/homedit/{id}', 'App\Http\Controllers\ApplicantController@updatehome')-&gt;name('homedit');
  • 每条路线都有不同的名称。所以对于 GET 请求,你需要以不同的方式命名它。 Route::post('/homedit/{id}', 'App\Http\Controllers\ApplicantController@updatehome')-&gt;name('homedit');Route::get('/homedit', 'App\Http\Controllers\ApplicantController@edithome')-&gt;name('anyothername');
【解决方案2】:

在 csrf 下方添加另一个输入字段,例如 name=id 和 value="{{$hm['id']}}" 因为没有在视图文件中找到id记录无法更新,找到记录并在id上更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多