samedi 14 septembre 2019

My models and my relationships on laravel

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)

enter image description here

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire