如何在控制器存储 Laravel 中传递和插入 id

在视野中


 <form action="{{route('memberPost', $courses->id)}}" method="POST">

        @csrf

        <button type="submit" class="btn clever-btn mb-30 w-100">Ikut course</button>

      </form>

在控制器中


public function detail_courses($slug){


    $courses = Course::where('slug', $slug)->first();

    return view('single-courses', compact('courses','id_courses'));

}

public function memberPost(Request $request, $id){

    $courses = Course::where('id', $id)->first();

    $id_courses = $courses->id;

    $member = Member::create([

        'user_id' => auth()->user()->id,

        'course_id' => $id_courses

    ]);

    return redirect()->route('detail_courses');

    


}

在途中


    Route::group(['middleware' => ['checkRole']], function () {

    Route::get('/single-courses/{slug}', 'PagesController@detail_courses')->name('detail_courses');

    Route::post('/single-courses/{slug}/{id}', 'PagesController@memberPost')->name('memberPost');

});

当我尝试上面的代码时,出现了这样的错误 Missing required parameters for [Route: memberPost] [URI: single-courses / {slug} / {id}]。



BIG阳
浏览 73回答 1
1回答

不负相思意

您名为memberPost的路由有另一个需要传递的参数($slug)Route::post('/single-courses/{slug}/{id}',&nbsp;'PagesController@memberPost')->name('memberPost');在表单操作的路线中,您还需要传递 slug{{route('memberPost',&nbsp;['slug'&nbsp;=>&nbsp;$courses->slug,&nbsp;'id'&nbsp;=>&nbsp;$courses->id])}}
打开App,查看更多内容
随时随地看视频慕课网APP