2
votes

I have two users: teachers and students, each of them has different attributes. For example: student has one class, teacher has many classes, etc.
I would like to inherit OctoberCMS Users plugin, and add two controllers with these additional attributes (one for students and another for teachers). So, I've created two models extending user model. these 2 models use the same users table, and I added two tables for teachers and students, but I can't link them with the user table.
This is what I did:

class Teacher extends User 
{
  public $hasOne=[
    'mobile'=>'??',
  ]
}

So, I want to add a mobile field and other fields to every teacher in the teachers table, how can I do this?
Please note that I don't want to add fields to the users table. I want the shared columns in user table, teacher additional fields in teacher table, and student additional fields in students table.

3

3 Answers

2
votes

Well, This is the solution I get:
1- Add the additional fields of teachers and students to the users table.
2- Let the teacher and student models extend the user model
3- Add a function to each model to add user group (teacher or student) before saving.
4- Apply a filter on the controllers of teachers and students to list only them groups.

This is the full code:

class UserAddFields extends Migration
{
public function up()
{
    if (Schema::hasColumn('users', 'mobile')) {
        return;
    }

    Schema::table('users', function($table)
    {
        $table->string('mobile', 100)->nullable();
        $table->integer('class_id', 100);
    });
}

}


class Teacher extends User
{
public $belongsToMany = [
    'groups' => [UserGroup::class, 'table' => 'users_groups', 'key' => 'user_id', 'otherKey' => 'user_group_id']
];

public function beforeCreate()
    {
            $this->groups=UserGroup::where('code','teachers')->first();
    }           
}

class Teachers extends Controller
{
public function listExtendQuery($query)
    {
            $query->whereHas('groups', function ($q){
                $q->where('code', '=', "teachers");
            });
    }
}

and the same for students.

1
votes

Based on my opinion, you should choose different flow than yours. As per my experience, I think you have to make two separate models and controllers for Teachers and Students with separate tables.

After that, you have to assign relations between Teacher, Student and User. For understanding relations, you can refer Octobercms relations.

As per my point of view, relations should be given as per below:

In Teacher and Student model,

public $belongsTo = [
    'user'             => [ 'RainLab\User\Models\User' ],
];

And Rainlab User plugin have Group model so you can assign Users in different groups like 'teacher' and 'student'.

And further if you want to add extra field like model you can use User Plus plugin also User Plus plugin.

0
votes

I used this video to add thee dependencies of each group Extending the User plugin

It works with only One Group, but when I try to add te custom fields with the same way, I get this error :

"Method ::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException: Error encoding model ..."