0
농담, joke_tags, tags 테이블이 있습니다. 그리고 농담 ID와 일치하는 모든 태그를 얻으려고합니다.Laravel belongsToMany, 해결할 수 없습니다.
class Joke extends Model
{
public function tags() {
return $this->belongsToMany('App\Tag', 'joke_tags', 'tag_id', 'joke_id');
}
}
내 태그 모델 :
class Tag extends Model
{
public function jokes() {
return $this->belongsToMany('App\Joke');
}
}
두 함수 tags()
및 jokes()
는 지금 2 시간 동안 그와 함께 어려움을 겪고 봤는데 ... 작동하지 않습니다.
내 DB 레이아웃 :
Schema::create('jokes', function (Blueprint $table) {
$table->increments('ID');
$table->string('author');
$table->longText('joke');
$table->timestamps();
});
Schema::create('joke_tags', function (Blueprint $table) {
$table->increments('ID');
$table->integer('joke_id')->unsigned();
$table->foreign('joke_id')->references('ID')->on('jokes');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('ID')->on('tags');
});
Schema::create('joke_votes', function (Blueprint $table) {
$table->increments('ID');
$table->integer('joke_id')->unsigned();
$table->foreign('joke_id')->references('ID')->on('jokes');
$table->string('IP');
$table->boolean('type');
});
Schema::create('tags', function (Blueprint $table) {
$table->increments('ID');
$table->string('tag');
});
는 왜 다른 기본 키를 가정 laravel합니까? ... – tempestor
는 laravel은 기본 키는 'ID'로 명명 된 가정 때문입니다. 당신은 그것을 바꾼 다음 당신이 무엇을 변경했는지 말할 필요가 있습니다. –
@tempestor이 답변이 도움이 되었습니까? 관계에서 잘못된 구문을 사용하는 것을 볼 수 있으므로 작동하지 않습니다. 이거 고쳐 봤어? –