2
votes

i am creating crud in laravel.i ran into the problem with Missing required parameters for [Route: employees.update] [URI: employees/{employee}]. (View: C:\xampp\htdocs\jbs\resources\views\employees\edit.blade.php)what i tried so far i attach below. i added the model view controller below

Edit Blade Page

  @extends('employees.layout')       
    @section('content')
        <div class="row">
            <div class="col-lg-12 margin-tb">
                <div class="pull-left">
                    <h2>Edit Employee</h2>
                </div>
                <div class="pull-right">
                    <a class="btn btn-primary" href="{{ route('employees.index') }}"> Back</a>
                </div>
            </div>
        </div>
       
        @if ($errors->any())
            <div class="alert alert-danger">
                <strong>Whoops!</strong> There were some problems with your input.<br><br>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif
      
        <form action="{{ route('employees.update',$employees->id) }}" method="POST">
            @csrf
            @method('PUT')
       
             <div class="row">
    
                <div class="col-xs-12 col-sm-12 col-md-12">
                    <div class="form-group">
                        <strong>StudentName:</strong>
                        <input type="text" name="name" value="{{ $employees->studname }}" class="form-control" placeholder="Name">
                    </div>
                </div>
                  <div class="col-xs-12 col-sm-12 col-md-12">
                                <div class="form-group">
                                    <strong>Course</strong>
                                    <input type="text" name="name" value="{{ $employees->course }}" class="form-control" placeholder="course">
                                </div>
                            </div>
                 <div class="col-xs-12 col-sm-12 col-md-12">
                                            <div class="form-group">
                                                <strong>Fee</strong>
                                                <input type="text" name="name" value="{{ $employees->fee }}" class="form-control" placeholder="fee">
                                            </div>
                                        </div>

                <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                  <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
       
        </form>
    @endsection

Controller

 public function edit(Employee $employees)
    {
        return view('employees.edit',compact('employees'));
    }



    public function update(Request $request, Employee $employees)
    {
        $request->validate([
            'studname' => 'required',
            'course' => 'required',
            'fee' => 'required',
        ]);

        $employees->update($request->all());

        return redirect()->route('employees.index')
            ->with('success','Employee updated successfully');
    }

Model

    protected $fillable = [
        'studname', 'course','fee',
    ];
}

enter image description here

routes

Route::resource('employees','App\Http\Controllers\EmployeeController');
3
Do you get the error after you submit or when you load the page? if you can still load the page you can try to inspect the action attribute. by the way the second parameter of the route helper must be an array, so you should write it like this route('employees.update', [$employees->id]) - Fajry Hamzah
Still my problem is not solved i tried 2 days still the same problem i tried all the ways - tutusfun

3 Answers

0
votes

Your route parameter is employee not employees. Route parameter names for resource routes are singular by default. The route is actually like this:

employees/{employee}

So in your edit method of your Controller you are using the wrong parameter name so you are getting a new instance of Employee instead of the Implicit Route Model Binding that would inject the instance based on the route parameter, you need to match the typehinted parameter name to the route parameter name:

//                            {employee}
public function edit(Employee $employee)
{
    ...
    return view(...., ['employee' => $employee]);
}

Now in the view you will have your actual existing Employee instance that you can use to generate the URL to the route instead of an empty Employee that does not have an id which was returning null:

{{ route('employees.update', ['employee' => $employee->id]) }}
// or
{{ route('employees.update', ['employee' => $employee]) }}

The route system can get the correct key from the model instance.

-1
votes

You should pass $employees->id as a hidden input field.

Make your route as

 Route::post('employee/update', 'YourController@yourFunction');

and in the edit page the form action should look like

<form action="{{ route('employees.update'}}" method="POST">

make a hidden input field for passing id

<input type="hidden" name="id" value="{{$employees->id}}"></input>
-1
votes

You should pass the route name to the route function like this

route('route name',parameter1)

Example :

Route::post('employee/update/{id}' ,YourController@update)->name('update_employee');

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