Behiry
Behiry

Reputation: 573

OctoberCMS - Update a field in a related table when inserting

In OctoberCMS, I have three tables:
Students, Courses and Subscriptions (studentID, courseID, active)
When student subscribes to a course, it's not activated until the admin activate it. That is logical. But When admin opens a course form from backend, he can select students to subscribe in this course, then they are added to subscriptions table. Here I would like to update the field: active automatically.
How can I dot that?
I wrote this code, but it didn't work:

    public function afterSave()
    {
        foreach($this->students as $student)
        {
            $student->pivot->is_activated = true;
            $student->pivot->save;
        }
    }

Edit:

This is all my code after @Hardik Satasiya advice:

    class Course extends Model
    {
      public $belongsToMany = [
        'students'=>[
                'Sunsoft\Courses\Models\Student', 
                'table'=>'sunsoft_courses_student_course', 
                'order'=>'users.name', 
                'scope'=>'students',
                'timestamps' => true,
                'pivot'=>['id', 'is_activated'],
                'pivotModel'=>'Sunsoft\Courses\Models\StudentCourse',
        ],
      ];

      public function afterSave()
      {
        foreach($this->students as $student)
        {
            $student->pivot->is_activated = true;
            $student->pivot->save;
            $student->save;
        }
      }
    }

    class Student extends User
    {
      public $belongsToMany = [
        'courses'=>[
            'sunsoft\courses\models\Course', 
            'table'=>'sunsoft_courses_student_course', 
            'order'=>'sunsoft_courses_courses.name',
            'pivot'=>['id', 'is_activated'],
            'timestamps' => true,
            'pivotModel'=>'Sunsoft\Courses\Models\StudentCourse',
        ],
      ];
     }

     class StudentCourse extends Model
     {
        public $belongsTo= [
          'course'=>['sunsoft\courses\models\Course'],
          'student'=>['sunsoft\courses\models\Student', 'scope'=>'students', 'order'=>'users.name'],
        ];      
    }

This is the error I get:
http://prntscr.com/jhrfzu

Upvotes: 0

Views: 769

Answers (1)

Hardik Satasiya
Hardik Satasiya

Reputation: 9693

For this you need to define proper relation ship inside the Courses Model

define Pivot Model => Subscriptions you already did it.

Now define proper Relation in Courses. Model

public $belongsToMany = [
    ....
    'students_pivot_model' => [
        'HardikSatasiya\Plugin\Models\Students', // replace model
        'table' => 'yournamespace_course_student', // replace tb name
        'pivot' => ['is_activated'],
        'timestamps' => true, // if you added times-stamp support
        'pivotModel' => 'HardikSatasiya\Plugin\Models\Subscriptions',
    ],
    ....
];

Now to set is_activated manually

public function afterSave()
{
    foreach($this->students_pivot_model as $pivot_model)
    {
        $pivot_model->pivot->is_activated = true;
        // no validations
        $pivot_model->pivot->forceSave(); 
    }
}

this should work. If any doubts please comment.

Upvotes: 2

Related Questions