我具有以下嵌套的资源定义:
routes / web.php
Route::resource('employees', 'EmployeeController');
Route::resource('employees.external_training_records', 'ExternalTrainingRecordController');
ExternalTrainingRecordController
public function create()
{
return view('employees.external_training_records.create');
}
视图/员工/show.blade.php
<a href="{{ route('employees.external_training_records.create', $employee) }}">Create External Training Record</a>
视图/员工/external_training_records/create.blade.php
<div class="card-header">{{ $employee->name }}</div>
Laravel不喜欢在views/employees/external_training_records/create.blade.php
中调用$employee
上的未定义变量
如何将$employee
传递到嵌套资源的创建页面?
答案 0 :(得分:0)
它可以解决您的问题。在 route 函数中添加一个变量名。
route('employees.external_training_records.create', ['employee' => $employee]);
ExternalTrainingRecordController
与这里相同
public function create(Employee $employee) // add parameter with type hint
{
return view('employees.external_training_records.create', ['employee' => $employee]); // add parameter in array
}