lundi 22 octobre 2018

How search collection if is more then one?

I have table with categories. Category A and Category B And i have a post table with post assign to category Now i can get all post and category

$posts = PostGroup::whereIn('name', ['A', 'B'])

PostGroup file has

public function posts() {
        return $this->hasMany('App\Dict');
    }

Ok now when i call code above i have i have good result

Collection {#301 ▼
  #items: array:2 [▼
    0 => PostGroup {#289 ▶}
    1 => PostGroup {#302 ▶}
  ]
}

I can do foreach or i can get posts

$posts = PostGroup::whereIn('name', ['A', 'B'])
$posts[0]->posts->where('group_id', 'A');

How can i avoid foreach and search collection $posts to find group A something like:

$posts->where('group_id', 'A')->posts;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire