猿问

Laravel route() 函数未将正确的类型传递给控制器

刀片代码:


<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">

控制器功能:


public function edit(Employee $employee)

{

    $companies = Company::get();

    return view('employee.edit', compact('employee', 'companies'));

}

错误:


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

路线:


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');

我正在尝试使应用程序成为多语言应用程序,因此在添加 lang 变量后,路由将不会传递 $employee->id 变量。我应该为 lang 变量添加一个可传递给控制器的变量吗?有什么解决办法吗?非常感谢。


月关宝盒
浏览 152回答 4
4回答

RISEBY

首先你可以制定一条改变语言的路线Route:: get('lang/{lang}', function ($locale) {&nbsp; &nbsp; session(['locale' => $locale]);&nbsp; &nbsp; return \Redirect::back();})第2步:中间件public function handle($request, Closure $next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; App::setLocale(session('locale'));&nbsp; &nbsp; &nbsp; &nbsp; return $next($request);&nbsp; &nbsp; }可以建群后Route::group(['middleware' => 'language'],function(){&nbsp; &nbsp; //routes with u want change language&nbsp; &nbsp; Route::get('/edit/{employee}', 'EmployeesController@edit')->name('edit');&nbsp; &nbsp; Route::patch('/edit/{employee}', 'EmployeesController@update')->name('update');});并且您忘记在每条路线中发送语言

料青山看我应如是

如果您也在路线中经过,locale那么应该如下所示:网页.phpRoute::get('your-Own-route/{lang}/{employee}','YourController@edit');控制器编辑方法public function edit($lang,Employee $employee){&nbsp; &nbsp; $companies = Company::get();&nbsp; &nbsp; return view('employee.edit', compact('employee', 'companies'));}

梦里花落0921

我认为你必须像这样修改你的路线在 web.php 中Route::get('your-route/{lang}/{id}','YourController@edit');在你的控制器中public function edit($lang,Employee $employee){&nbsp; &nbsp; $companies = Company::get();&nbsp; &nbsp; return view('employee.edit', compact('employee', 'companies'));}

慕后森

你的参数不对。正如堆栈跟踪所示,控制器方法需要Employee模型的实例,但您正在传递一个字符串改变public function edit(Employee $employee)到public function edit(Request $request, $employeeId) // you can remove $request if you dont intend to perform redirects所以最后你的代码看起来像public function edit(Request $request, $employeeId){&nbsp; $employee = Employee::find($employeeId);&nbsp; $companies = Company::all(); // use all instead of get if you arent going to perform selections.&nbsp; return view('employee.edit', compact('employee', 'companies'));}$employeeId注:您可能需要处理根据提供的信息找不到员工的情况
随时随地看视频慕课网APP
我要回答