【发布时间】:2020-08-23 06:16:00
【问题描述】:
你好家人我正在处理一个学校管理项目,但我的关系有问题: 我有一张年份表、学生表、水平表 现在一个学生在一个学年内只能在一个级别注册一次, 在一个学年中,几个学生可以注册一个级别
- table eleves belongsToMany table niveaux
- table niveau belongsToMany table eleves
- table eleve_niveau 属于 table annees
- table 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', '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');
}
}
模型等级
<?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');
}
}
我为数据透视表做了一个模型
<?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');
}
}
我无法插入数据透视表的问题我不知道从哪个模型开始 如果我使用高模型注册一个级别的学生,我必须通过枢轴模型 有没有可能???
【问题讨论】:
-
有点不清楚,您可以添加更多与您的问题相关的解释吗?
标签: laravel eloquent-relationship