Ali
Ali

Reputation: 15

Laravel route() function doesn't pass the right type to the controller

The blade code:

<td><a href="{{ route('employee.show', ['lang' => app()->getLocale(), 'employee' => $employee->id]) }}">{{ $employee->first_name }} {{ $employee->last_name }}</a></td>
                                <td><a href="{{ route('employee.update', ['lang' => app()->getLocale(), 'employee' => $employee->id]) }}" class="btn btn-primary">{{ __('app-text.indexEdit') }}</a></td>
                                <td><form action="{{ route('employee.delete', ['lang' => app()->getLocale(), 'employee' => $employee->id]) }}" method="post">

The controller function:

public function edit(Employee $employee)
{
    $companies = Company::get();
    return view('employee.edit', compact('employee', 'companies'));
}

The error:

TypeError
Argument 1 passed to App\Http\Controllers\EmployeesController::edit() must be an instance of App\Employee, string given
http://localhost:8000/fr/employee/edit/1

The routes:

Route::group(['prefix' => '{lang}'], function() {

    Route::prefix('employee')->name('employee.')->group(function() {
        
        Route::get('/edit/{employee}', 'EmployeesController@edit')->name('edit');
        Route::patch('/edit/{employee}', 'EmployeesController@update')->name('update');

I'm trying to make the application a multi-language application so just after I added the lang variable the route won't pass the $employee->id variable. Should I add a variable that's passable to my controller for the lang variable? any solution? Many thanks.

Upvotes: 0

Views: 929

Answers (4)

Luis Aguilar
Luis Aguilar

Reputation: 56

first you can make a route to change language

Route:: get('lang/{lang}', function ($locale) {
    session(['locale' => $locale]);
    return \Redirect::back();
})

step 2: middleware

public function handle($request, Closure $next)
    {
        App::setLocale(session('locale'));
        return $next($request);
    }

after you can make a group

Route::group(['middleware' => 'language'],function(){
    //routes with u want change language
    Route::get('/edit/{employee}', 'EmployeesController@edit')->name('edit');
    Route::patch('/edit/{employee}', 'EmployeesController@update')->name('update');
});

and you forget to send the language in each route

Upvotes: 1

Naveed Ali
Naveed Ali

Reputation: 1045

if you are passing locale as well in the route then it should be as below:

web.php

Route::get('your-Own-route/{lang}/{employee}','YourController@edit');

Controller edit method

public function edit($lang,Employee $employee)
{
    $companies = Company::get();
    return view('employee.edit', compact('employee', 'companies'));
}

Upvotes: 0

Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

I think you have to modify your routes like these

in web.php

Route::get('your-route/{lang}/{id}','YourController@edit');

In your controller

public function edit($lang,Employee $employee)
{
    $companies = Company::get();
    return view('employee.edit', compact('employee', 'companies'));
}

Upvotes: 0

Dev Man
Dev Man

Reputation: 2137

Your parameters are wrong. As the stack trace says the controller method is expecting an instance of your Employee model but you are passing in a string

Change

public function edit(Employee $employee)

To

public function edit(Request $request, $employeeId) // you can remove $request if you dont intend to perform redirects

So in the end your code looks like

public function edit(Request $request, $employeeId)
{
  $employee = Employee::find($employeeId);
  $companies = Company::all(); // use all instead of get if you arent going to perform selections.
  return view('employee.edit', compact('employee', 'companies'));
}

Note: you may need to handle cases where employee is not found based on the $employeeId supplied

Upvotes: 0

Related Questions