Code: Select all
Route::get('/tag/{tag_id}', [ArticlesController::class, 'tag'])->name('tag');
Code: Select all
public function tag($tag_id)
{
$tag = Tag::firstWhere('id', $tag_id);
$articles = Article::where('id', $tag->id)->orderBy('id', 'desc')->paginate($this->per_page);
return view(
'themes/' . $this->theme_directory . '/templates/index',
array_merge($this->data, [
'tag' => $tag,
'articles' => $articles
])
);
}
< /code>
Es besteht eine Viele-zu-Viele-Beziehung zwischen Artikeln und Tags. Ich habe einen Artikel_Tag
Im Tag Modell habe ich:
Code: Select all
class Tag extends Model
{
use HasFactory;
protected $fillable = ['name'];
public function article()
{
return $this->belongsToMany(Article::class);
}
}
hinzugefügt
Code: Select all
public function tags()
{
return $this->belongsToMany(Tag::class)->as('tags');
}
Die/Tag/1 Zeigt nur einen Artikel an, und dieser Artikel ist nicht enthält Ein Tag mit der ID von 1. /> [*] Welche funktionierende Lösung erfordert eine minimale Änderung im aktuellen Code?