【问题标题】:laravel relaion many to manylaravel 关系多对多
【发布时间】: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


【解决方案1】:

你读过文档吗?你用的是哪个版本的 laravel?

【讨论】:

    【解决方案2】:

    我在 laravel 中使用版本 7
    我阅读了 laravel 文档,发现了一个附加的方法,所以在我的模型级别上我去了 外键属性有我的两个模型

    hasMany('App\EleveNiveau', 'eleve_id'); // } 公共函数 niveaux() { 返回 $this->belongsToMany(Niveau::class)->withPivot('annee_id'); } } belongsTo('App\Annee'); } /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ // 公共函数 eleveNiveaus() // { // return $this->hasMany('App\EleveNiveau'); // } 公共函数 eleves() { return $this->belongsToMany(Eleve::class)->withPivot('annee_id'); } } 我认为现在可以使用另一种方法来管理包含 3 个辅助键的数据透视表

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 2018-01-22
      • 2016-02-26
      • 2016-01-04
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多