Hello, It looks like you're not saving changes to the user model. Try with:
$user = User::where('id', $request['student_id']);
$user->course_id = $course->id;
$user->save();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I have 2 Models:
User with the attribute of type string type
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->integer('enrolled_courses')->unsigned()->nullable();
$table->integer('course_id')->unsigned()->nullable();
$table->string('type');
$table->rememberToken();
$table->timestamps();
});
Schema for model Course:
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('student_id')->unsigned()->nullable();
$table->integer('tutor_id')->unsigned()->nullable();
$table->dateTime('date_begin');
$table->dateTime('date_end');
$table->string('spoken_language');
$table->text('description');
$table->timestamps();
});
I want to be able to distinguish between Student and Tutor with the attribute type
I am getting the Tutors and Students with the following method:
public function index()
{
$tutors = User::get()->where('type', 'tutor');
$students = User::get()->where('type', 'student');
return view('course.create.index', compact(['tutors', 'students']));
}
with a foreach loop I am rendering them with their Ids.
Relationships between the models
Model User
public function courses()
{
return $this->hasMany(Course::class);
}
Model Course
public function students()
{
return $this->belongsTo(User::class)->where('type', 'student');
}
public function tutors()
{
return $this->belongsTo(User::class)->where('type', 'tutor');
}
There is an admin who create Courses with the following method:
public function store(AdminCreateNewCourseRequest $request)
{
$this->authorize('create-course');
$course = new Course;
$course->name = $request->name;
$course->tutor_id = $request->tutor_id;
$course->student_id = $request->student_id;
$course->description = $request->description;
$course->save();
$user = User::where('id', $request['student_id']);
$user->course_id = $course->id;
return redirect($course->path())
->with('flash', 'The course has been published');
}
I am trying to assign the Id of the course to the student or tutor.
It doesn't work and my relationships are not working as well
I am sorry for the long explanation
Thanks
$student = User::find($request->student_id);
$student->course_id = $course->id;
$student->save();
But, a student can only be on one course?
You already store student_id on the course, there is no need to store it on the user also.
To really help, just explain the relationships.
By the way. You can have a Student model and a Tutor model both referencing the same users table and using a scope to apply your where clause
Please or to participate in this conversation.