laravel关系到很多

问题描述

你好家人,我正在做一个学校管理项目,但是与人际关系存在问题: 我有一张年表,学生,水平 现在,一个学生在一个学年中只能在一个级别中注册一次, 在一个学年中,几个学生可以报名参加课程

  • 表元素属于多个表niveaux
  • 表niveau属于多表元素
  • 表eleve_niveau属于表annees
  • 表中的annees有很多eleve_niveau

现在,在我的模型级别,我做了他的事情:

膝盖模型

<?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','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');
    }
}

模型元素

<?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','updated_at'];

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

我为数据透视表做了一个模型

<?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','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');
    }
}

我无法设法插入数据透视表中的问题,我不知道从哪个模型开始 如果我使用高级模型在某个级别注册学生,则必须通过数据透视模型 有没有可能????

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)