Hey,
It's a little bit more complicated! First you need to check if max_price is bigger than min_price, if this happens you need to swap them! Same goes for weight!
And for the brand i think you need to use orWhere or whereIn because you want to select products that are from x brand or y, not to choose products that are from brand x and brand y
I think my question was a little bit misleading.
Let's say i have a method under 'Phones' called 'number' which returns a number 'call it'. I want to filter the phones based on a methods number.
Is that possible in laravel?
My code by now
Controller
if (isset($min_price)){
$query->has('minprice','>=',$min_price);
}
if (isset($max_price)){
$query->has('maxprice','<=',$max_price);
}
Phone Model
public function productnum()
{
return $this->hasMany('App\Product');
}
public function minprice(){
return $this->productnum->min('newprice');
}
public function maxprice(){
return $this->productnum->max('newprice');
}
and i am getting
FatalErrorException in Builder.php line 564:
Call to a member function getRelationCountQuery() on a non-object
micahaza said:
Do you have models? This is not the rigth approach I think.
Thanks for the reply micahaza
What is not the right approach?
I do have models. Phone and Product. Phone hasmany Product.
try
$max_price=...;
$min_price=...;
$phones = Phone::whereHas('productnum', function($q) use($max_price, $min_price)
{
$q->where('minprice','>=',$min_price)
->where('maxprice' ,'<=', $max_price);
})->get();
cemleme said:
try
$max_price=...; $min_price=...; $phones = Phone::whereHas('productnum', function($q) use($max_price, $min_price) { $q->where('minprice','>=',$min_price) ->where('maxprice' ,'<=', $max_price); })->get();
noo, that didn't work either. (I can understand why though - where checks for sql rows) i believe the way to solve this is with 'has'. But i dont understand what is wrong with it :(
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community