I am building a blog with laravel where a post has many tags. I want to filter all post by a tag. means if I click on "PHP" tag I want to get all associated post.
Here is my code
I have two table first for tags and a second table for the link with posts
tag_table
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('tags');
$table->timestamps();
});
}
relation tag table
public function up()
{
Schema::create('article_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
});
}
Article Model
class Article extends Model
{
public function tags()
{
return $this->belongsToMany('App\Tag');
}
}
tag model
class Tag extends Model
{
public function articles()
{
return $this->belongsToMany('App\Article');
}
}
Tag Controller
public function show($id,$name)
{
//here I received tag id and name.
$list->with('articles')->get();
return view('articles.tagshow')->withList($list);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire