To demonstrate my problem, I created a dummy database with these 4 tables (defined as Eloquent models): User, Product, ShoppingItem (short: Item), Order; with all their relevant relationships. (User is included for completeness only)
Products:
Orders:
- ID
- user_id
- the date
- hasMany (item)
Products:
- ID
- product_id
- order_id
- belongsTo (Product)
- belongsTo (Order)
Thus, (Shopping) Items records are created whenever a customer purchases certain products. An order (trading card) is also created and contains 1 or more items belonging to exactly one client (User) and his current shopping process.
Now this problem is connected with the list of products and the way of analyzing their "success". How often are they sold and when was the last time they were sold?
I can, for example, use the following query to get all Orders for a single product and, therefore, calculate how often they are sold:
$orders = Order::whereHas('items', function ($query) use ($id) { $query->where('product_id', $id) ->where('date', '<', Carbon::now()); })->orderBy('date', 'desc')->get();
In addition, I can get a list of all the products ordered by the amount of time they were sold with this:
$products = Products::withCount('items') ->orderBy('item_count', 'desc');
But I want to get a list of all products, sorted by the date when they were last ordered (bought). This result should be an Eloquent query query or a set of Query Builder, so that I can use pagination.
I have a mutator defined in my product model, for example:
public function getLastTimeSoldAttribute( $value ) { $id = $this->id; // get list of plans using this song $order = Order::whereHas('items', function ($query) use ($id) { $query->where('product_id', $id) ->where('date', '<', Carbon::now()); })->orderBy('date', 'desc')->first(); return $order->date; }
It returns the last purchase date of this product, and I can use it in the view as follows:
{{ $product->last_time_sold }}
However, I cannot use 'last_time_sold' in an Eloquent OrderBy statement!
Is there any other way to get the "last_time_sold" value of a product in a database relationship as described above without losing the ability to paginate?
Ultimately, what is the correct way to request a product table and order products the last time they were ordered?
Remember that the elements do not have a date, only the order (Trade Card) has a date.