Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unkown.persone's avatar

Assign value to a Model's table

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

0 likes
4 replies
ENGEL's avatar

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();
ENGEL's avatar

@unkown.persone

  1. According to your schemes a course can be taught only by one tutor and taken only by one student.
  2. One Student have multiples course but one Course has only one student.

So I think that you maybe should use Many to Many relationship. https://laravel.com/docs/5.4/eloquent-relationships#many-to-many

On the other hand the issue with the assignment of the course id to the user model may be caused by:

$user = User::where('id', $request['student_id']); //remove this line and
$user = User::where('id', $request->student_id)->first(); //replace for this other
$user->course_id = $course->id;
$user->save();
Snapey's avatar
Snapey
Best Answer
Level 122
$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.

  • A Tutor has many courses?
  • A course has one tutor?
  • A course has many students?
  • A student has many courses?

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

1 like

Please or to participate in this conversation.