LARAVEL:当我想更新数据库时,参数太少了

发布于 2025-01-22 04:00:23 字数 3696 浏览 0 评论 0 原文

我必须为酒店开发预订系统。 您首先必须创建用户,而不是预订。 可以选择编辑注册用户,但这是我卡住的部分。 我遵循一个非常好的教程,但是目前我无法从他的视频,互联网或文档中弄清楚我的错误。.

我已经使用了帖子方法将新数据插入数据库中,但是要编辑数据并进行更新,我必须使用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']);

缓存 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>

I have to develop a reservation system for a hotel.
You first have to create a user and than you can make a reservation.
There is an option to edit the registered users but that is the part where I'm stuck.
I follow a very good tutorial but at the moment I can't figure out my error from his video or the internet or documentation..

I've used the post method to insert new data inside my database but to edit the data and update it, I have to use the put method. But it gives an error "Too few arguments in my function .. 1 passed in and 2 expected".

I am aware I have to cache the routes at every change!!

This is my controller:

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')
    ]);
}

These are my routes:

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']);

And the file I have make the changes is (using the post method at first but inserted a put method):

 <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2025-01-29 04:00:23
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">

我也鼓励您检查路由

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

Controller functions:

    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')
    ]);
}

in the form:

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

also i would encourage you to check the documentation for Laravel Validation and Routing

甚是思念 2025-01-29 04:00:23

将此代码用于路由:

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

‌我希望它将有用

Use this code for routes :

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

Use this code for Controller: ‌

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');
}

in form :

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

Use this code to display errors when updating: ‌

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

I hope it will be useful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文