Laravel 8 多对多更改自定义属性的 id

Posted

技术标签:

【中文标题】Laravel 8 多对多更改自定义属性的 id【英文标题】:Laravel8 Many To Many change id for custom attribute 【发布时间】:2022-01-08 08:19:05 【问题描述】:

我在FacultiesDepartments 之间创建了关系many-to-many(我正在处理原型时间表问题)。对于请愿,这些表格需要验证码vcode。问题出在这个关系上,我不知道为什么这个关系只识别 id's 表的雄辩查询而不是 vcode

附上我的迁移和模型关系。

移民系表

public function up()

    Schema::create('faculties', function (Blueprint $table) 
        $table->id();
        $table->unsignedBigInteger('vcode')->index()->unique();
        $table->string('code');
        $table->string('name');
        $table->string('website');
        $table->string('email');
        $table->string('phone');
        $table->timestamps();
    );

教师模型的关系店

public function departments()

    return $this->belongsToMany(Department::class, 'faculty_departments','faculty_vcode','department_vcode');
 

移民部门表

public function up()

    Schema::create('departments', function (Blueprint $table) 
        $table->id();
        $table->unsignedBigInteger('vcode')->index()->unique();
        $table->string('code');
        $table->string('name');
        $table->string('phone');
        $table->string('email');
        $table->string('website');
        $table->timestamps();
    );

部门模型的关系商店

public function faculties()

    return $this->belongsToMany(Faculty::class, 'faculty_departments', 'department_vcode','faculty_vcode',);

Migration FacultyDepartments(关系)

public function up()

    Schema::create('faculty_departments', function (Blueprint $table) 
        $table->id();
        $table->unsignedBigInteger('faculty_vcode')->nullable()->index();
        $table->foreign('faculty_vcode')
              ->references('vcode')
              ->on('faculties')
              ->cascadeOnUpdate()
              ->nullOnDelete();
        $table->unsignedBigInteger('department_vcode')->nullable()->index();
        $table->foreign('department_vcode')
              ->references('vcode')
              ->on('departments')
              ->cascadeOnUpdate()
              ->nullOnDelete();
        $table->timestamps();
    );

我在获取查询时引发错误并测试了错误(获取 id´s 表)

faculty::with('departments')->get() 给我:

select `departments`.*, `faculty_departments`.`faculty_vcode` as `pivot_faculty_vcode`, `faculty_departments`.`department_vcode` as `pivot_department_vcode` 
from `departments` 
inner join `faculty_departments` on `departments`.`id` = `faculty_departments`.`department_vcode` 
 
where `faculty_departments`.`faculty_vcode` in (1, 2, 3, 4, 5, 6, 7)

【问题讨论】:

如果你更容易,还有SO in Spanish 【参考方案1】:

由于您没有为 faculties 表或 departments 表提供字段名称,Laravel 将假定它是这些表的主键 (id)。

您需要将 belongsToMany 方法更新为:

public function departments()

    return $this->belongsToMany(
        Department::class,      // The related model
        'faculty_departments',  // The intermediate (pivot) table name
        'faculty_vcode',        // The pivot field relating to faculties
        'department_vcode',     // The pivot field relating to departments
        'vcode',                // The field on the faculties table
        'vcode',                // The field on the departments table
    );

【讨论】:

谢谢你的回答。 @Rob91 如果这回答了您的问题,请您将其标记为已接受。谢谢:)

以上是关于Laravel 8 多对多更改自定义属性的 id的主要内容,如果未能解决你的问题,请参考以下文章

TypeORM:使用自定义属性查询多对多

从 Laravel 的 belongsToMany 关系中选择自定义列

Laravel 8:在空的多对多关系中使用枢轴时出错

Laravel 与 uuid 的多对多关系返回总是空的

通过联结表进行多对多自连接

实体框架自引用分层多对多