Filtering Eloquent on relation
For a new project I am working on a webshop. I have products with a one to many relationship with plant details. Plant details have a column price. I need to order the products on the price of the first detail.
If I use a join I get all products ordered by price but I get a new product for each detail relation.
I tried:
$products = $products->get(); $products = $products->sortBy(function ($product, $key) { return $product->plantDetails()->first()->price; }); $products = $this->paginate($products, $perPage = 24, true);
with $this->paginate being: public function paginate($items, $perPage = 15, $page = null, $options = []) { $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); $items = $items instanceof Collection ? $items : Collection::make($items); return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); }
but than I don't get the current page in the pagination link. Does anyone have any ideas?
Please or to participate in this conversation.