Search code examples
phplaravelcrud

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


I am getting error at {{ route('one_one.update',$student->id) }} and I am unable to use edit for CRUD operation because of this error.

Route (I am using resource controller so my route is this)

Route::resource('/one_one',StudentController::class);

edit.blade.php

<form method="POST" action="{{ route('one_one.update',$student->id) }}" enctype="multipart/form-data">
        @csrf
        @method('PUT')
        Name <input type="text" name="name" value="{{ $student->name }}"><br><br>
        Contact <input type="text" name="contact" value="{{ $student->contact }}"><br><br>
        Email <input type="text" name="email" value="{{ $student->email }}"><br><br>
    <h3>Student Details</h3>
        Alternative contact <input type="text" name="alter_contact" value="{{ $student->studentdetail->alter_contact }}"><br><br>
        Address <input type="text" name="address" value="{{ $student->studentdetail->address }}"><br><br>
        Course <input type="text" name="course" value="{{ $student->studentdetail->course }}"><br><br>
        <button type="submit">Submit</button>
    </form>

index.balde.php

<form action="{{ route('one_one.edit',$student->id) }}"><button type="submit">Edit</button></form>
                <form action="{{ route('one_one.destroy',$student->id) }}" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="submit">Delete</button></form>

Controller

public function edit(Student $student)
    {
        return view('one_one.edit',compact('student'));
    }
    public function update(Request $request, Student $student)
    {
        $student=Student::update([
            'name'=>$request->name,
            'contact'=>$request->contact,
            'email'=>$request->email,
        ]);

        $student->studentdetail()->update([
            'alter_contact'=>$request->alter_contact,
            'address'=>$request->address,
            'course'=>$request->course,
        ]);
        return redirect()->route('one_one.index')->with('success','Data updated successfully');
    }

I also tried to pass $student->id as array but it is also not working.


Solution

  • You are passing an empty non-existing model to your view. There is no Implicit Route Model Binding happening for your edit method. The route parameter name is not matching the typehinted parameter of the Controller's edit method. You are getting method injection (a new instance of a Student model) instead of Implicit Route Model Binding

    Your route parameter for your resource routes is one_one(as that is the name of the resource) not student. Your method signature would have to be adjusted for Implicit Route Model Binding:

    public function edit(Student $one_one)
    

    Laravel 10.x Docs - Routing - Route Model Binding - Implicit Binding