【问题标题】:Error Many to Many Relationship in Laravel 5.8Laravel 5.8 中的多对多关系错误
【发布时间】:2021-07-06 02:58:27
【问题描述】:

当我尝试在 Laravel 中检索多对多关系数据时出现错误。错误信息是

SQLSTATE[23000]:完整性约束违规:1052 where 子句中的列“id”不明确(SQL:select * from jobs where(不存在(select * from job_typesinner join job_types_jobs on @987654326 @.id = job_types_jobs.job_types_id 其中jobs.id = job_types_jobs.jobs_idjob_types.job_types.deleted_at 为空)或存在(从@9876543 中选择* *在job_types.id 上加入job_types_jobs = job_types_jobs.job_types_id 其中jobs.id = job_types_jobs.jobs_idjob_types_jobs = 3 和job_types_jobsjob_types_jobs = 3 和@98为空)和name LIKE %%)和jobs.deleted_at 为空)

表格

JobTypes.php

public function jobs() // children  
{
    // one type of job has many jobs
    return $this->belongsToMany('App\Jobs'); // id refer to jobs.id
}

Jobs.php

public function job_types() // parent 
{
    // one job only belongs to one type of job
    return $this->belongsToMany('App\jobTypes');
}

JobTypeController.php

public function assignJob($id)
{
    $jobType = \App\JobTypes::withTrashed()->find($id);
    $jobs = $jobType->jobs;

    return view('job-types.assign-job', compact(['jobType', 'jobs']));
}

查看

<label for="name">Job</label>
                <select selected="selected" multiple class="form-control {{ $errors->first('jobs') ? "is-invalid" : "" }}" name="jobs[]" id="jobs" class="jobs"></select>
                <div class="invalid-feedback">
                    {{ $errors->first('jobs') }}
                </div>

迁移

Schema::create('job_types', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('jobs', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('jobs_job_types', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('jobs_id')->unsigned()->index();
        $table->integer('job_types_id')->unsigned()->index();

        $table->foreign('jobs_id')->references('id')->on('jobs')->onDelete('cascade');
        $table->foreign('job_types_id')->references('id')->on('job_types')->onDelete('cascade');
    });

【问题讨论】:

  • 你的JobTypes模型真的叫jobTypesJobTypes吗?在Jobs.php 中更改它。另外,为什么parentbelongsToMany的关系——和child一样?
  • 它是JobTypes。父母或孩子只是对我的描述。
  • 但不应该是belongsToMany for Job -> JobTypehasMany for JobType -> Job?这将代表关系的双方。请尝试一下,看看这是否能解决或改变您的问题。
  • ``` 未找到列:1054 'where 子句'中的未知列 'jobs.job_types_id'(SQL:select * from jobs where jobs.job_types_id = 3 和 @987654373 @.job_types_id 不为空,jobs.deleted_at 为空)```
  • 您的迁移情况如何?

标签: laravel laravel-5 relational-database relationship


【解决方案1】:

您偏离了 Eloquent 用来识别不同表中的字段的默认命名方案。目前,您在列中使用复数形式(例如 jobs_id),但 Eloquent 假定使用单数形式 (´job_id`)。

job_job_type 迁移文件中,将所有列名以及架构名称更改为单数 名称。在运行migrate:refresh(即更新数据库架构)后,Eloquent 将能够自动将关系映射到数据库上的架构。

Schema::create('job_job_type', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('job_id')->unsigned()->index();
  $table->integer('job_type_id')->unsigned()->index();
    
  $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade');
  $table->foreign('job_type_id')->references('id')->on('job_types')->onDelete('cascade');
});

【讨论】:

  • 太棒了!虽然上面的代码没有错。一个错误,因为另一个代码......但无论如何,谢谢!
猜你喜欢
  • 2021-09-23
  • 2019-11-17
  • 2021-11-21
  • 2016-11-24
  • 2018-02-06
  • 2019-08-31
  • 2020-05-13
  • 1970-01-01
  • 2019-12-11
相关资源
最近更新 更多