I got an error when I try to retrieve many to many relationship data in Laravel. The message of error is
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (SQL: select * from
jobs
where (not exists (select * fromjob_types
inner joinjob_types_jobs
onjob_types
.id
=job_types_jobs
.job_types_id
wherejobs
.id
=job_types_jobs
.jobs_id
andjob_types
.deleted_at
is null) or exists (select * fromjob_types
inner joinjob_types_jobs
onjob_types
.id
=job_types_jobs
.job_types_id
wherejobs
.id
=job_types_jobs
.jobs_id
andid
= 3 andjob_types
.deleted_at
is null) andname
LIKE %%) andjobs
.deleted_at
is null)
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']));
}
View
<label for="name">Job</label>
<select selected="selected" multiple class="form-control " name="jobs[]" id="jobs" class="jobs"></select>
<div class="invalid-feedback">
</div>
Migarations
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');
});
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3mAiTt8
via IFTTT
Aucun commentaire:
Enregistrer un commentaire