Laravel: 当我想更新我的数据库时参数太少无法运行

Laravel: Too few arguments to function when I want to update my database

我必须开发一个酒店预订系统。 您首先必须创建一个用户,然后才能进行预订。 有一个选项可以编辑注册用户,但这是我卡住的部分。 我遵循了一个非常好的教程,但目前我无法从他的视频或互联网或文档中找出我的错误..

我使用 post 方法在我的数据库中插入新数据,但要编辑和更新数据,我必须使用 put 方法。但它给出了一个错误“我的函数中的参数太少 .. 1 个传入和 2 个预期”。

我知道我必须在每次更改时缓存路由!!

这是我的控制器:

public function store(Request $request) 
{
    $guest = Guest::create
    ([
        'titel' => $request->input('title'),
        'voornaam' => $request->input('fname'),
        'achternaam' => $request->input('sname'),
        'adres' => $request->input('address'),
        'postcode' => $request->input('zipcode'),
        'stad' =>  $request->input('city'),
        'provincie' => $request->input('provincie'), 
        'email' => $request->input('email')    
    ]);

    return redirect('./clients.html');
}



public function edit($id) 
{
    $guest = Guest::find($id);
    return view('edit')->with('guest', $guest);
}



public function update(Request $request, $id)
{
    $guest = Guest::where('id', $id)
    ->update([
        'titel' => $request->input('title'),
        'voornaam' => $request->input('fname'),
        'achternaam' => $request->input('sname'),
        'adres' => $request->input('address'),
        'postcode' => $request->input('zipcode'),
        'stad' =>  $request->input('city'),
        'provincie' => $request->input('provincie'), 
        'email' => $request->input('email')
    ]);
}

这些是我的路线:

Route::get('/', [PagesController::class, 'home']);

Route::get('/home.html', [PagesController::class, 'home']);

Route::get('/clients.html', [GuestsController::class, 'saveGuests']);

Route::post('/clients.html', [GuestsController::class, 'store']);

Route::put('/clients.html', [GuestsController::class, 'update']);

Route::get('/reservations.html', [PagesController::class, 'reservations']);

Route::get('/clients_new.html', [GuestsController::class, 'newclients']);

Route::get('/clients/{id}/edit.html', [GuestsController::class, 'edit']);

Route::get('/reservations_new.html', [PagesController::class, 'newReservations']);

我修改的文件是(最初使用post方法但插入了一个put方法):

 <form action="/clients.html" method="POST" class="w-3/12 m-auto flex flex-col justify-evenly items-center text-xl">
        @csrf
        @method('PUT')
        <div class="medium-6 columns py-4 text-3xl w-full flex flex-row items-center justify-between">
          <label class="font-semibold">Titel</label>
          <select name="title" class="w-22 border-solid border-2 border-grey-200 rounded-lg bg-gray-100 p-2" value="{{ $guest->titel }}">
            <option value="Mr." selected="selected">Mr.</option>
            <option value="Ms.">Mw.</option>
            <option value="Mrs.">Juf.</option>
            <option value="Dr.">Dr.</option>
          </select>
        </div>
        <div class="medium-6 columns m-auto py-4 text-3xl w-full flex flex-row items-center justify-between">
          <label class="font-semibold">Voornaam</label>
          <input name="fname" type="text" class="border-solid border-2 border-grey-200 rounded-lg bg-gray-100 p-2 " value="{{ $guest->voornaam }}">
        </div>
        <div class="medium-6 columns m-auto py-4 text-3xl w-full flex flex-row items-center justify-between">
          <label class="font-semibold">Achternaam</label>
          <input name="sname" type="text" class="border-solid border-2 border-grey-200 rounded-lg bg-gray-100 p-2" value="{{ $guest->achternaam }}">
        </div>
Route::put('/clients/{id}', [GuestsController::class, 'update']);
Route::get('/clients/{id}/edit', [GuestsController::class, 'edit']);

控制器功能:

    public function edit($id) 
{
    $guest = Guest::find($id);
    return view('edit',['guest'=> $guest]);
}

    public function update(Request $request, $id)
{
    $guest = Guest::where('id', $id)
    ->update([
        'titel' => $request->input('title'),
        'voornaam' => $request->input('fname'),
        'achternaam' => $request->input('sname'),
        'adres' => $request->input('address'),
        'postcode' => $request->input('zipcode'),
        'stad' =>  $request->input('city'),
        'provincie' => $request->input('provincie'), 
        'email' => $request->input('email')
    ]);
}

形式为:

<form action="{{url("clients",$guest->id)}}" method="POST" class="w-3/12 m-auto flex flex-col justify-evenly items-center text-xl">

我还建议您查看 Laravel Validation and Routing

的文档

将此代码用于路线:

Route::put('/clients/{id}', [GuestsController::class, 'update']);
Route::get('/clients/{id}/edit', [GuestsController::class, 'edit']);

控制器使用此代码:‌

public function edit($id)
{
    $guest = Guest::query()->find($id);
    return view('edit',['guest'=> $guest]);
}

public function update(Request $request, $id)
{
    $guest = Guest::query()->where('id', $id)->first();

    if (!$guest){
        Redirect()->back()->withErrors(['msg' => 'guest not found']);
    }

    $guest->update([
        'titel' => $request->input('title'),
        'voornaam' => $request->input('fname'),
        'achternaam' => $request->input('sname'),
        'adres' => $request->input('address'),
        'postcode' => $request->input('zipcode'),
        'stad' =>  $request->input('city'),
        'provincie' => $request->input('provincie'),
        'email' => $request->input('email')
    ]);

    return redirect()
        ->action([GuestsController::class, 'edit'])
        ->with('success', 'Product updated successfully');
}

形式:

<form action="{{url("clients",$guest->id)}}" method="POST" class="w-3/12 m-auto flex flex-col justify-evenly items-center text-xl">

更新时使用此代码显示错误:‌

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif

希望有用