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

david2000's avatar

My architecture is correct?

My goal is for each user to log in and have their remarks after each seance. I have two roles, an administrator and a candidate.

My tables are :

Candidates with 3 fields (id, name, email)

Seances with 3 fields (id, date_seance, fk_candidate)

Remarks with 4 fields (id, instruction, description, fk_seance)

Users with 3 fields (id, email, password)

My frist question, is it my relationship are ok according you ???

Model User

public function candidates()
    {
        return $this->hasOne('App\Candidate', 'user_id', 'id');
    }

    public function remarks()
    {
        return $this->hasOne('App\Remark', 'user_id', 'id');
    }

Model Candidate

public function user(){
        return $this->belongsTo('App\User', 'id', 'user_id');
    }

    public function seances(){

        return $this->hasMany('App\Seance', 'fk_candidate');
    }

Model Seance

public function candidates(){

        return $this->belongsTo('App\Candidate', 'fk_candidate');
    }

    public function remarks(){

        return $this->hasMany('App\Remark', 'fk_seance');
    }

Model Remark

public function user(){
        return $this->belongsTo('App\User', 'id', 'user_id');
    }
    
    public function seances(){

        return $this->belongsTo('App\Seance', 'fk_seance');
    }

My second question, please is it I must to add a user_id on the table Remarks ???

Remarks

Schema::create('remarks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('instruction', 30);
            $table->text('description', 80);
            $table->integer('fk_seance')->unsigned();
            $table->foreign('fk_seance')->references('id')->on('seances');
            $table->timestamps();
        });

Candidates

Schema::create('candidates', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users');
            $table->string('name');
            $table->string('email');
            $table->timestamps();
        });

Seances

Schema::create('seances', function (Blueprint $table) {
            $table->increments('id');
            $table->date('date_seance');
            $table->integer('fk_candidate')->unsigned();
            $table->foreign('fk_candidate')->references('id')->on('candidates');
            $table->timestamps();
        });

Thank you a lot for your help.

0 likes
4 replies
Snapey's avatar

Anything that is belongsTo needs a key on its table, pointing to the other table, unless it is a belongsToMany where the pivot table has the keys

Just write it out as pairs of statements. For instance

Seance has many remarks, remark belongs to Seance

Candidate..something.. Seance, Seance..something.. Candidate

I can't work it out for you since you don't describe the relationships sufficiently. Also you say you have Administrators and Candidates but then talk about users

Snapey's avatar

Sorry, I must be being thick. I don't understand your cardinalities.

Seance only has one remark ?

Please or to participate in this conversation.