Load Your Public Assets With Versioning in Laravel

Hello, devs

In this quick tutorial, I will show you asset versioning in laravel. So that means, from this tutorial you will learn how to get the last updated timestamp with your public assets. To create this, I will create my custom helper function.

We can create this using laravel mix version. But in this tutorial, i don't use laravel mix. I will use custom method class to create or handle this assets version in laravel.

This helper will help you to serve the assets which are last updated by the server. The main aim of this helper is to eliminate the cache of the assets we updated in the server. Let's see the example code.

App\Helper\Helper.php

namespace App\Helper;

class Helper
{
     /**
     * Generate a URL to an application asset with a versioned timestamp parameter.
     * @param $path
     * @param null $secure
     * @return string
     */

    public static function assetPathWithVersioning($path, $secure = null)
    {
        $timestamp = @filemtime(public_path($path)) ?: 0;
        return asset($path, $secure) . '?' . $timestamp;
    }
}

 

Now call this method in your script and CSS file like below:

 

Now your assets will be loaded like that:

 

Read also: How to Use Union Query with Laravel Query Builder

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js