laravellaravel-6

Issue with enrolling student in online course application


I am making an application in Laravel for online courses. I am getting this error when enrolling a user to the course.

Integrity constraint violation: 1048 Column 'course_id' cannot be null (SQL: insert into `course_user` (`course_id`, `user_id`) values (?, 2))

This is my route

Route::post('/subscription/{course_id}', 'SubscriptionsController@enrolle')->name('enrolle');

Subscription controller

public function enrolle(Course $course)
{
    auth()->user()->courses()->attach($course);

    return redirect()->back();
}

Blade form

<form method="post" action="/subscription/{{$courses->id}}">
    {{csrf_field()}}
    <input type="submit" class="site-btn buy-btn" value="Enroll" >
</form>

Solution

  • You're Route Parameter is wrong it must be course. The Route Parameter and the Controller Parameter must have the same name.

    Route::post('/subscription/{course}', 'SubscriptionsController@enrolle')->name('enrolle');