Show version information in your Laravel project

It might be helpful to show git & versioning information in Laravel. Let me show you the easy way.

You can show it for yourself only, put in somewhere for your users to find, or use the information in forms to provide/gather useful feedback.

What are we doing?

The following snippets will allow you to show the following information:

  • Git hash, preceded with the current version tag, if available.
  • Current environment (eg. local, staging, production)
  • Laravel version
  • Current app locale
  • PHP version

A config file for git information

The following config file gathers some useful information from the git history, on HEAD:

  • the current tag
  • the latest commit hash
  • the date for that hash

It also provides a string key to show everything in one piece.

// config/version.php

$tag = exec('git describe --tags --abbrev=0');
if (empty($tag)) {
    $tag = '-.-.-';

$hash = trim(exec('git log --pretty="%h" -n1 HEAD'));
$date = Carbon\Carbon::parse(trim(exec('git log -n1 --pretty=%ci HEAD')));

return [
  'tag' => $tag,
  'date' => $date,
  'hash' => $hash,
  'string' => sprintf('%s-%s (%s)', $tag, $hash, $date->format('d/m/y H:i'));

Showing it on the front-end

// resources/views/partials/version.blade.php

  <span>{{ config('version.string') }}</span>
  <span>{{ config('app.env') }}</span>
  <span>{{ 'Laravel ' . app()->version() }}</span>
  <span>{{ app()->getLocale() }}</span>
  <span>{{ 'PHP ' . PHP_VERSION }}</span>

Feel free to adjust these to your needs and styling.


Include tags in git history

You might want to append --tags to the git pull command in your deployment script for the tags to show up. (I didn't seem to need this.)

Cache to optimize in production

It's also useful to cache your configuration in order to optimize for production.

Just don't forget to bust the cache during deployment:

php artisan config:clear
php artisan config:cache

Addition of cache busting commands & --pull: ref. @brbcoding

Helpful resources to adjust for your needs

Post last updated: November 16, 2021