php - Laravel query builder with pivot table -
i have 2 tables pivot table
table tours
id | name | country_id | featured
table countries
id | name
pivot table country_tour
id | country_id | tour_id
i want to find tour has featured column of tours table set 1 , country_id of country_tour table set 1.
updated:
you can using laravel's query builder method - wherehas():
your models should (many many relationships):
tour model:
<?php namespace app; use illuminate\database\eloquent\model; class tour extends model { public function countries() { return $this->belongstomany('app\country'); } } and country model
<?php namespace app; use illuminate\database\eloquent\model; class country extends model { public function tours() { return $this->belongstomany('app\tour'); } } and can fetch desired results using below query:
tour::where('featured', 1) ->wherehas('countries', function($q) { $q->where('id', 1); }) ->get(); this collection of tours featured = 1 , having country id = 1.
hope helps!
Comments
Post a Comment