1

I have a project for food ordering. On my object(restaurant) show page I'm calling all product categories with relationship for foreach products that belongs to that category.

I'm also using laravel livewire and this is my Component:

<?php

namespace App\Http\Livewire\Stores;

use Livewire\Component;

use Illuminate\Http\Request;

use App\Models\Store;
use App\Models\CategoryProduct;

use App\Models\Product;
use App\Http\Livewire\Cart;

use App\Models\Favourite;

use Session;
use Auth;

class Show extends Component
{
    public $store;
    public $categories;
    public $ratings;

    public $message = '';
    public $supplements = [];
    public $supplementsArray = [];

    public $features = [];
    public $featuresArray = '';

    public $quantity = 1;

    public $deliveryPrice = 0;

    public function render(Request $request)
    {
        if(!Session::has('cart')){
            return view('livewire.stores.show');
        }

        $items = explode('|', $this->featuresArray);

        $oldCart = Session::get('cart');

        $store_id = $this->store->id;

        $oldCart->deliveryPrice = $this->store->delivery_price;

        if($oldCart->storeId != $store_id){
            $request->session()->forget('cart');
        }

        $cart = new Cart($oldCart);

        //dd(session()->get('cart'));

        return view('livewire.stores.show',[
            'products' => $cart->items,
            'totalPrice' => $cart->totalPrice + $cart->deliveryPrice,
            'totalQty' => $cart->totalQty,
            'ordersPrice' => $cart->ordersPrice,
            'storeId' => $cart->storeId,
        ]);
    } 
}

This public $categories; variable is from my Controller:

     /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $store = Store::find($id);
        $categories = CategoryProduct::where('store_id', $store->id)->get();

        $ratings = $store->getAllRatings($store->id);

        if($store->checkWorktime() == 'Zatvoreno'){
            return view('website.stores.closed', compact('store', 'categories', 'ratings'));
        }

        return view('website.stores.show', compact('store', 'categories', 'ratings'));
    }

And in my blade view this is how I'm viewing it:

@foreach($categories as $category)
   <div class="row m-0" id="{{$category->id}}">
      <h6 class="p-3 m-0 bg-light font-weight-bold w-100">{{$category->title}} <span class="float-right"> 
         <small class="text-black-50">{{count($category->categoryProducts)}} proizvoda</small></span> 
      </h6>
      <div class="col-md-12 px-0 border-top">
         <div class="">
            <ul class="list-group list-group-flush">
               @foreach($category->categoryProducts as $product)                               
                  <a href="#0" class="list-group-item list-group-item-action" data-toggle="modal" data-target="#modal{{$product->id}}">
                      <div class="row">
                         <div class="col-9">
                            <b class="text-dark">{{$product->title}}</b><br>
                            <small class="text-muted">{{$product->description}}</small><br><br>
                            @if($product->features->isNotEmpty())
                            <small class="text-dark" style="font-size:14px !important;">od {{$product->price}}&euro;</small>
                            @else
                            <small class="text-dark" style="font-size:14px !important;">{{$product->price}}&euro;</small>
                            @endif
                         </div>
                         @if($product->thumbnail)
                            <div class="col-3">
                            <span class="float-right">
                            <img src="{{$product->thumbnail->getUrl('showPreview')}}" style="width:80px;height:80px;border-radius:50%;object-fit:cover;" alt="">
                            </span>
                            </div>
                         @endif
                      </div>
                   </a>
                   @include('website.stores.product-modal')                                                          
                @endforeach
             </ul>
          </div>
       </div>
    </div>
@endforeach

And my page is loading crazy slow. I know I'm something doing wrong and I know that there is a way to speed this up. How can I do that?

Milos
  • 552
  • 2
  • 7
  • 32

0 Answers0