Delano van londen
Delano van londen

Reputation: 416

Laravel: Too few arguments to function 0 passed and 1 expected

I am currently working on a 1 page crud for a laravel project. I had a lot of trouble to begin with, but now the last error i have is

Too few arguments to function 0 passed and 1 expected

this happens on loading the main page of the crud. this file gives the error: edit.blade.php

<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
    @method('PATCH') 
    @csrf
    <div class="form-group">
 
        <label for="name"> Name:</label>
        <input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
    </div>
 
    <div class="form-group">
        <label for="email">email </label>
        <input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
    </div>
 
   
    <button type="submit" class="btn btn-primary">Update</button>
</form>

controller index:

 public function index($id)
{
    $users = User::all();
    $userEdit = User::find($id);
     return view('admin.user.index', compact('users', 'userEdit'));
}

yes i need $users and $userEdit, but i cant reach $userEdit on the first load of the page. normally there is also no $id in index() but i added it to try to fix it, this did not work

Upvotes: 0

Views: 1993

Answers (2)

scorpions78
scorpions78

Reputation: 558

you have a error in your form:

<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
     @csrf
    @method('PUT')
    <div class="form-group">
 
        <label for="name"> Name:</label>
        <input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
    </div>
 
    <div class="form-group">
        <label for="email">email </label>
        <input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
    </div>
 
   
    <button type="submit" class="btn btn-primary">Update</button>
</form>

method not it´s PATCH it´s PUT and firts send your CSRF

Upvotes: 0

aynber
aynber

Reputation: 23010

You need to pass the parameter in as an array with a key specifying the route's parameter name. See https://laravel.com/docs/9.x/routing#generating-urls-to-named-routes

<form method="post" action="{{ route('admin.user.update', ['id' => $userEdit->id]) }}">

Upvotes: 3

Related Questions