我試圖anINT從這個 URL: myapp.build/courses/anINT(在 中實現CoursesController) 傳遞到下面$id的Lesson_unitsController函數中。我嘗試了很多解決方案,但似乎無法解決問題。CoursesController 中實現 url 的函數是: public function show($id){ $course = Course::find($id); return view('courses.show')->with('course', $course);}show.blade.php 文件的一部分是:@if(!Auth::guest()) @if(Auth::user()->id == $course->user_id) <a href="/courses/{{$course->id}}/edit" class="btn btn-outline-secondary btn-light">Edit Course</a> <a href="/lesson_units/specificindex" class="btn btn-outline-secondary btn-light">Lesson Units</a> {!!Form::open(['action'=> ['CoursesController@destroy', $course->id], 'method' => 'POST', 'class' => 'float-right'])!!} {{Form::hidden('_method', 'DELETE')}} {{Form::submit('Delete', ['class' => 'btn btn-danger'])}} {!!Form::close()!!} @endif@endif該Lesson_unitsController功能是: public function index(){ $lesson_units = Lesson_unit::orderBy('title','asc')->paginate(10); return view('lesson_units.index')->with('lesson_units', $lesson_units);}public function specificindex($id){ $course = Course::find($id); return view('lesson_units.specificindex')->with('lesson_units', $course->lesson_units);}web.php 中的路由是:Route::resource('courses', 'CoursesController');Route::resource('lesson_units', 'Lesson_unitsController');Route::get('/courses/{id}', 'Lesson_unitsController@specificIndex');我想的是,鏈接,當Lesson Units點擊了網頁上,在idurl中傳遞給specificindex該函數Lesson_unitsController?,F在,我只得到一個空白頁。我究竟做錯了什么?
2 回答

楊__羊羊
TA貢獻1943條經驗 獲得超7個贊
您沒有設置路由來處理傳入。Route類中$id的資源方法將提供一個到您的控制器的GET路由,而不需要任何變量。它是默認的索引路由,默認情況下不傳遞變量。Lesson_unitsController
有幾種方法可以做到這一點,但最簡單的方法是為您的特定需求創建一條新路線:
Route::get('lesson_units/{id}', 'Lesson_unitsController@specificIndex');
然后specificIndex使用傳入變量在控制器中創建函數:
public function specialIndex($id)
{
$course = Course::find($id);
// return view to whatever you like
}
HTH
- 2 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消