2

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

2
  • I am not sure though. but so far I guess, is class 'item' your eloquent class or normal classs? Commented Apr 8, 2019 at 9:10
  • Its an Eloquent class (a model) Commented Apr 8, 2019 at 9:10

1 Answer 1

2

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

 $result = Model::get()->filter(function($item) {
    return $item->weekly_sales_count > 3;
 });
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, let me try it but the mission is to filter the ones with the highest sales and paginate them, so whats the proper way of using filter. If you can link me to where filter is explained i will be so happy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.