Laravel - Eloquent Search on Join but returning multiple rows, need one -
so have 2 models product , productpricehistories
product hasmany productpricehistories , it's daily price of product.
i have 1 slight problem, when try find lowest price of product i'm returned multiple of same products because of join finding multiple prices product.
this query:
$products = productpricehistory::orderby( 'product_price_histories.price', $request->get('direction') ) ->with('products') ->select('products.*') ->paginate(16); i have tried various ways, need products in order of expensive or cheapest based on $request->get('direction')
if it's not possible in eloquent in db:raw surely it's me not knowing how properly.
you can provide closure every element in with() method can order eager loaded products , even, mentioned, first one:
->with(['products' => function($query) use ($request) { $query->orderby('orderbycolumn', $request->get('direction')) ->first(); // if want first result }]) hope helps you.
Comments
Post a Comment