Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

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

Last updated 9 years ago.
0

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?

0

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
Last updated 9 years ago.
0

Do you have models? This is not the rigth approach I think.

0

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.

Last updated 9 years ago.
0

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();
Last updated 9 years ago.
0

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 :(

0

Anyone?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

George george Joined 10 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.