将变量传递给刀片中的嵌套资源

时间:2018-09-16 09:08:31

标签: php laravel laravel-blade

我具有以下嵌套的资源定义:

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传递到嵌套资源的创建页面?

1 个答案:

答案 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
}