Laravel 8: Missing required parameter for [Route: students.update]

Viewed 529

So I'm trying to understand laravel since I'm new to it so I'm creating a simple crud application... I'm kinda stuck with the editing part.

I'm having this error:

Missing required parameter for [Route: students.update] [URI: students/{student}] [Missing parameter: student]

I'm pretty sure I passed a parameter which is the id of the selected row. Pls, help.tnx

Here's my controller:

 public function edit(Students $students)
    {
        return view('students.edit', compact('students'));
    }

public function update(Request $request, Students $students)
    {
        $this->validate($request, [
            'fname' => 'required',
            'lname' => 'required',
            'age' => 'required',
            'course' => 'required'
        ]);
        $students->update($request->all());
        return redirect()->route('students.index')->with('success','Student updated successfully');
    }

Here's the view where the Edit button is

@foreach($students as $student)
          <tr>
            *some codes*
            <td>
                <a href="{{ route('students.edit', $student->id) }}" class="btn btn-success" id="editBtn">Edit</a>          
            </td>
          </tr>
        @endforeach

And here is the view of the Edit

<form method="POST" action="{{ route('students.update', $students->id) }}">
        @csrf
        @method('PUT')

        *some codes*

        <input type="submit" id="update" class="btn btn-success" value="Update"/>
</form>
1 Answers

I think you are passing $students->id and expecting student object. try to pass $students as object.

<form method="POST" action="{{ route('students.update', $students) }}">
        @csrf
        @method('PUT')

        *some codes*

        <input type="submit" id="update" class="btn btn-success" value="Update"></button>
</form>

Same thing for editing:

<a href="{{ route('students.edit', $student) }}" class="btn btn-success" id="editBtn">Edit</a>
Related