mercredi 30 novembre 2016

Laravel Intersection of Model Data

I have Article and Tag models with corresponding functions in each files:

Article.php:

public function tags()
    {
        return $this->belongsToMany('App\Tag')->withTimestamps();
    }

Tag.php:

public function articles()
    {
        return $this->belongsToMany('App\Article');
    }

On Blade view, I can access articles with a corresponding tag by calling

@foreach($tag->articles as $article)
   ...
@endforeach

From above, I can access a number of articles for a single tag. How can we filter articles with multiple tags, say only articles which have both tag1 and tag2 in a Tag model?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire