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

Time:04-16

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" >
        @csrf
        @method('PUT')
        <div >
          <label >Titel</label>
          <select name="title"  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 >
          <label >Voornaam</label>
          <input name="fname" type="text"  value="{{ $guest->voornaam }}">
        </div>
        <div >
          <label >Achternaam</label>
          <input name="sname" type="text"  value="{{ $guest->achternaam }}">
        </div>

CodePudding user response:

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

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

  • Related