亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在控制器 Laravel 5.8.22 中使用來自 URL 的參數

在控制器 Laravel 5.8.22 中使用來自 URL 的參數

PHP
aluckdog 2021-08-27 16:33:30
我試圖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


查看完整回答
反對 回復 2021-08-27
  • 2 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號