-1

hello family I am working on a school management project but I have a problem with relations: I have a table of years, students, level now a pupil can register only once in a level during a school year, during a school year several pupils can register for a level

  • table eleves belongToMany table niveaux
  • table niveau belongToMany table eleves
  • table eleve_niveau belongTo table annees
  • table annees has many eleve_niveau

Now at the level of my models I made his:

model annee

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $id
 * @property string $debut_annee
 * @property string $fin_annee
 * @property string $annee_scolaire
 * @property string $created_at
 * @property string $updated_at
 * @property EleveNiveau[] $eleveNiveaus
 * @property Niveau[] $niveauxes
 */
class Annee extends Model
{
    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'integer';

    /**
     * @var array
     */
    protected $fillable = ['debut_annee', 'fin_annee', 'annee_scolaire', 'created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function eleveNiveaus()
    {
        return $this->hasMany('App\EleveNiveau');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function niveauxes()
    {
        return $this->hasMany('App\Niveau');
    }
}

model niveau

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $id
 * @property integer $annee_id
 * @property string $nom_niveau
 * @property string $branche
 * @property string $created_at
 * @property string $updated_at
 * @property Annee $annee
 * @property EleveNiveau[] $eleveNiveaus
 */
class Niveau extends Model
{
    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'integer';

    /**
     * @var array
     */
    protected $fillable = ['annee_id', 'nom_niveau', 'branche', 'created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function annee()
    {
        return $this->belongsTo('App\Annee');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function eleveNiveaus()
    {
        return $this->hasMany('App\EleveNiveau');
    }
}

model eleves

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $id
 * @property string $pv
 * @property string $nom
 * @property string $prenom
 * @property string $created_at
 * @property string $updated_at
 * @property EleveNiveau[] $eleveNiveaus
 */
class Eleve extends Model
{
    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'integer';

    /**
     * @var array
     */
    protected $fillable = ['pv', 'nom', 'prenom', 'created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function eleveNiveaus()
    {
        return $this->hasMany('App\EleveNiveau', 'eleve_id');
    }
}

I did a model for the pivot table

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * @property integer $eleve_id
 * @property integer $niveau_id
 * @property integer $annee_id
 * @property string $date_inscription
 * @property string $created_at
 * @property string $updated_at
 * @property Annee $annee
 * @property Elefe $elefe
 * @property Niveau $niveau
 */
class EleveNiveau extends Model
{
    /**
     * The table associated with the model.
     * 
     * @var string
     */
    protected $table = 'eleve_niveau';

    /**
     * @var array
     */
    protected $fillable = ['date_inscription', 'created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function annee()
    {
        return $this->belongsTo('App\Annee');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function elefe()
    {
        return $this->belongsTo('App\Elefe', 'eleve_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function niveau()
    {
        return $this->belongsTo('App\Niveau');
    }
}

the problem I can't manage to insert into the pivot table I don't know which model to start with if I use the high model to register a pupil at a level, I have to go through the pivot model is there a possibility ????

HeySora
  • 846
  • 7
  • 19

2 Answers2

0

Did you reed the documentaion? Which version of laravel are you using?

A.Rahman
  • 45
  • 1
  • 11
0

I use version 7 in laravel
I read the laravel documentation I found an attached method so at the level of my models I went the foreign key attribute has my two models

hasMany('App\EleveNiveau', 'eleve_id'); // } public function niveaux() { return $this->belongsToMany(Niveau::class)->withPivot('annee_id'); } } belongsTo('App\Annee'); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ // public function eleveNiveaus() // { // return $this->hasMany('App\EleveNiveau'); // } public function eleves() { return $this->belongsToMany(Eleve::class)->withPivot('annee_id'); } } I think it works now there is another method to manage a pivot table which contains 3 secondary keys