4

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>
  • you are missing the id of the Guest Route::put('/clients.html{id}', [GuestsController::class, 'update']); and in your form action /clients.html/{$guest->id} – Znar Apr 15 '22 at 22:46
  • How can I implement the id of the Guest in a correct way? But I don't see where I should insert it.. –  Apr 15 '22 at 22:50
  • in you edit function at your controller pass the guest to your view return view('edit',['guest' => $guest); and then you can access it in your blade file this how I prefer to pass values to blade file – Znar Apr 15 '22 at 22:52
  • Is not working. I get a 404 Not found with this as the URL: http://127.0.0.1:8000/clients.html/%7B$guest-%3Eid%7D –  Apr 15 '22 at 22:54
  • 1
    Because of your help, i got it working!! I will post the answer and give you cridentials! –  Apr 15 '22 at 22:59

2 Answers2

1
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

Znar
  • 369
  • 2
  • 11
0

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

AlirezaAhmadi
  • 72
  • 1
  • 11