I have a jokes, joke_tags, tags table. And I'm trying to get all tags that match the joke ID.
class Joke extends Model
{
public function tags() {
return $this->belongsToMany('App\Tag', 'joke_tags', 'tag_id', 'joke_id');
}
}
My Tag model:
class Tag extends Model
{
public function jokes() {
return $this->belongsToMany('App\Joke');
}
}
Both functions tags()
and jokes()
don't work... I've been struggling with that for 2 hours now.
My db layout:
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');
});