Flash
Flash

Reputation: 1125

Laravel query by custom attribute

I have a question pertaining querying an eloquent model using a custom attribute.

I have a class/model Item and i have created an attribute:

  function getWeeklySalesCountAttribute()
  {
     return rand(3, 1000); //real logic is in db
  }

So in my controller I want to pick items that have the highest weekly sales first and paginate them

    $items = Item::where(function($item){
        //find the items with highest weekly sales
    })->paginate(10); 

How do i achieve that given I manage to pull the $item->weekly_sales_count attribute

Upvotes: 2

Views: 4431

Answers (1)

Manuel Robles
Manuel Robles

Reputation: 121

You can't use Eloquent, you need use a filter.

 $result = Model::get()->filter(function($item) {
    return $item->weekly_sales_count > 3;
 });

Upvotes: 2

Related Questions