Commit 833c0e38 authored by Artem's avatar Artem

make eager loading on orders page and reduce db queries in cart model by updating current items

parent e52ef3b4
...@@ -67,9 +67,20 @@ class Cart /*extends Model*/ ...@@ -67,9 +67,20 @@ class Cart /*extends Model*/
public function updateCurrentItems() public function updateCurrentItems()
{ {
// foreach ($this->items as $id => $item) {
// $this->items[$id]['item'] = Product::where('id', $id)->first();
// }
// TODO ask if this is good solution for perfromance improvement
$productsIdArr = [];
foreach ($this->items as $id => $item) { foreach ($this->items as $id => $item) {
$this->items[$id]['item'] = Product::where('id', $id)->first(); $productsIdArr[] = $id;
} }
$products = Product::whereIn('id', $productsIdArr)->get();
foreach ($products as $product) {
$this->items[$product->id]['item'] = $product;
}
$this->updateTotalPrice(); $this->updateTotalPrice();
} }
} }
...@@ -14,7 +14,11 @@ class ProfileController extends Controller ...@@ -14,7 +14,11 @@ class ProfileController extends Controller
public function showOrders() public function showOrders()
{ {
$orders = Auth::user()->orders()->orderBy('id', 'desc')->paginate(10); $orders = Auth::user()->orders()
->with('products') // TODO ask if this is correct use
->orderBy('id', 'desc')
->paginate(10);
return view('profile.orders', compact('orders')); return view('profile.orders', compact('orders'));
} }
......
...@@ -23,6 +23,18 @@ class AppServiceProvider extends ServiceProvider ...@@ -23,6 +23,18 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
// // \Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
//// dump($query->sql);
//// dump($query->bindings);
//// dump($query->time);
// });
// \DB::listen(function($query) {
// \Log::info(
//// $query->sql,
//// $query->bindings,
// $query->time
// );
// });
} }
} }
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
@foreach($order->products as $product) @foreach($order->products as $product)
<li class="list-group-item order-item"> <li class="list-group-item order-item">
<a href="{{ route('product.show', $product->product_id) }}" <a href="{{ route('product.show', $product->product_id) }}"
{{-- href="{{ $product->product->path() }}" TODO ask if this is good for performance, to get product from eloquent relation --}}
>{{ $product->ordered_reference }}</a> >{{ $product->ordered_reference }}</a>
<div class="d-flex"> <div class="d-flex">
<div>{{ $product->ordered_price }} EUR</div> <div>{{ $product->ordered_price }} EUR</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment