Home > Software engineering >  How to improve speed on my laravel website?
How to improve speed on my laravel website?

Time:01-27

I built a website using Laravel and it's extremely slow.

This the code and the files for the website:

web.php:

Route::get('/product/{url}', 'FrontendController@getProduct')->where('url', '^[\w.-]*$');

FrontendController:

    public function getProduct(String $url){
    $product = Product::where('url', $url)->first();
    $imgs = ProductImage::all()->where('product_id',$product->id);
    return view('frontend.product', ['product' => $product, 'products' => Product::all(), 'images' => $imgs ]);
}

product.blade.php:

   <div  style="margin-top:15rem">
    <div >
        <div >
            <a href=""  style="padding-right: 5rem">TALK WITH A DESIGNER</a>
            <!-- HOURS CONDITION -->
            @php
            $dt = new DateTime("now", new DateTimeZone('Portugal'));
            @endphp

            @if ($dt->format('H:i:s') >= '09:00:00' && $dt->format('H:i:s') <= '18:00:00')
                <a  data-toggle="modal" data-target="#contact-five-minutes-modal">CALL ME IN 5 MINUTES</a>
            @else
                <a  data-toggle="modal" data-target="#contact-us-modal">CONTACT US</a>
            @endif
        </div>
    </div>
    <div >
        <div  style="border-right: 1px solid rgba(177, 177, 177, 0.8);">
            <div >
                <div >
                    @foreach ($images as $image)
                    <div ><img src="{{ $image->url }}" alt="" ></div>
                    @endforeach
                </div>
              </div>
        </div>
        <div >
            <div >
                <div >
                    @foreach ($images as $image)
                    <div ><img src="{{ $image->url }}" alt="" ></div>
                    @endforeach
                </div>                
                <div ></div>
                <div ></div>
            </div>
        </div>
        <div  style="padding-left: 1.5rem !important">
            <p >meet</p>
            <h1  style="font-weight: 500">{{ $product->name }}</h1>
            <h4  style="font-weight: 500">by {{ $product->brand }}</h4>
            <p >{{ $product->description }}</p>
            <h5 >MATERIAL AND FINISHES:</h5>
            <p >{{ $product->finishes }}</p>
            <h5 >DIMENSIONS</h5>
            <p >{{ $product->dimensions }}</p>
            <a  style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#downloadCatalogue" data-bs-toggle="modal" data-bs-target="#downloadCatalogue">DOWNLOAD CATALOGUE</a>
            <a  style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#productSheet{{ $product->id}}" data-bs-toggle="modal" data-bs-target="#productSheet{{ $product->id}}">DOWNLOAD PRODUCT SHEET</a>
            <a  style="margin-top:2.5rem;margin-bottom:2.5rem; display:block;" href="#reqCustom{{ $product->id}}" data-bs-toggle="modal" data-bs-target="#reqCustom{{ $product->id}}">REQUEST CUSTOMIZATION</a>
            <button  data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->id }}">GET PRICE</button>
        </div>
    </div>    
    <div >
        <div  style="margin-top:6rem">
            <h1  style="font-weight: 600">Related Products</h1>
        </div>
    </div>
    <div >
        <div >
            <img src="{{ $product->related_product_img_1}}" alt="" >
        </div>
        <div >
            <img src="{{ $product->related_product_img_2}}"  alt="" >
        </div>
        <div >
            <img src="{{ $product->related_product_img_3}}"  alt="" >
        </div>
        <div >
            <img src="{{ $product->related_product_img_4}}"  alt="" >
        </div>
    </div>
    <div  style="margin-bottom: 5rem">
        @php
            $slug_1 = str_replace(' ', '-', $product->related_product_name_1);
            $url_1 = strtolower( $slug_1 );
            $slug_2 = str_replace(' ', '-', $product->related_product_name_2);
            $url_2 = strtolower( $slug_2 );
            $slug_3 = str_replace(' ', '-', $product->related_product_name_3);
            $url_3 = strtolower( $slug_3 );
            $slug_4 = str_replace(' ', '-', $product->related_product_name_4);
            $url_4 = strtolower( $slug_4 );
        @endphp
        <div >
            <div >
                <p >new</p>
                <a href="/product/{{ $url_1 }}"><h4 >{{ $product->related_product_name_1}}</h4></a>
                <a  href="#getPrice{{ $product->related_product_id_1}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_1}}">get price</a>
            </div>
        </div>
        <div >
            <div >
                <p >new</p>
                <a href="/product/{{ $url_2 }}"><h4 >{{ $product->related_product_name_2}}</h4></a>
                <a  href="#getPrice{{ $product->related_product_id_2}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_2}}">get price</a>
            </div>
        </div>
        <div >
            <div >
                <p >new</p>
                <a href="/product/{{ $url_3 }}"><h4 >{{ $product->related_product_name_3}}</h4></a>
                <a  href="#getPrice{{ $product->related_product_id_3}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_3}}">get price</a>
            </div>
        </div>
        <div >
            <div >
                <p >new</p>
                <a href="/product/{{ $url_4 }}"><h4 >{{ $product->related_product_name_4}}</h4></a>
                <a  href="#getPrice{{ $product->related_product_id_4}}" data-bs-toggle="modal" data-bs-target="#getPrice{{ $product->related_product_id_4}}">get price</a>
            </div>
        </div>
    </div>
</div>

These are the results shown on a speed test website:

enter image description here

enter image description here

I am a beginner, so I´ve been studying ways to improve this. Do any of you have suggestions on how to improve the website performance? Thanks in advance.

CodePudding user response:

Solved it by changing the following code and removing a few bootstrap modals from the product.blade.

FrontEndController:

public function getProductTest(String $url){
    $product = Product::where('url', $url)->first();
    $imgs = ProductImage::where('product_id',$product->id)->get();
    return view('frontend.product-test', ['product' => $product, 'images' => $imgs ]);
}

The speed went from almost eleven seconds to 1.22 seconds.

CodePudding user response:

First change the below code in your frontendController

$imgs = ProductImage::where('product_id',$product->id)->get();

This will improve your query loading time.

Then in the below code

return view('frontend.product', ['product' => $product, 'products' => Product::all(), 'images' => $imgs ]);

why are you passing all the products to blade

'products' => Product::all(),

This is a bad idea, and it must be slowing down your page only get what you need. calling everything in database is really not a good idea

  •  Tags:  
  • Related