Introduction

Laravel apps read sensitive information from their .env file.

Recently I found out that Laravel Mix can pass values from the same .env file to the js portion of your app as long as they are prefixed with MIX_

I use Gitlab ci pipelines to build production assets so that I dont need that additional tooling on the production servers the main one being:

  • yarn run production

This is preceded with cp .env.example .env meaning when the build commands are being run, they are going to use values from the .example.env file.

If your project doesn’t make use of anything from the .env file then this is totally fine, however in scenarios where you do, since production applications will almost certainly have different .env values to those in the .example.env file (Never commit credentials to source control!) the resulting file will have been built with the wrong credentials.

In this article I’m going to show how you can use gitlab CI to build those assets with updated environmental variables so that they function as expected when deployed to your production servers.

Continue reading

Introduction

Laravel’s Form Requests are a great way of removing validation logic from your controllers.

There are times were it can be useful to update or change the request data before it is passed to the validator for example formatting postcodes, removing invalid characters or providing default values to data.

The official documentation shows how we can perform additional logic after the rule sets have been run but not before hand.

Digging through the Form Request api there is mention of a method called prepareForValidation which is an empty method that is called before the actual validation rules are run as can be seen in implementation:

the prepare for validation method

So given that the method is empty how do we use it and go about updating the form request data?

Continue reading

Introduction and Prerequisites

I’ve been remote working full time for over 3 years now.

In that time I’ve had to work with a number of clients who restrict access to their internal systems via IP Address whitelisting.

As a developer who often works from different locations and countries this can quickly become problematic.

A paid for solution is to purchase a Dedicated Static IP VPN from a reputable provider like NordVPN. and ask the clients IT team to whitelist your new Dedicated Static IP.

This is something I’ve been doing for a while and whilst the initial set up is a little fiddly, (You have to set up 2 accounts and get their team to link them together) its worked flawlessly for me.

However if you have the following:

  • SSH access to a device in a location with a Static IP (Perhaps you get one from your ISP or your office has one)
  • You use Linux or OSX (Sorry windows guys I don’t think even with WSL this will work).

Then you can use the super handy command line tool SSHUTTLE to route all (or some) of your traffic through that device.

Lets take a look at getting it working…

Continue reading

One of the projects I’ve been working recently has involved writing a system to communicate with a Clients pre-existing Legacy system. The system doesn’t have the ability to “talk out” but can be queried using a SOAP service.

The legacy system is:

  • Slow to interact with
  • Prone to crashing regularly

Here’s how we dealt with those 2 issues:

Since we don’t want to impact our end users experience the project has been set up to make use of Laravel queuing system with Redis and those queues are configured with Laravel Horizon.

When these jobs failed for whatever reason - including the clients system going down we wanted to be notified so that someone could look into what was happening.

The team at spatie.be created a great package to do that handle notifying us when a failed job occurred.

However there are multiple jobs being run concurrently that run as frequently as every minute to check for things on their server. Which mean’t that once the legacy system went down we would receive multiple notifications until the system came back up, (In some cases this has been hours - not ideal).

After a while being flooded with these sorts of notifications makes them become an annoyance rather than useful and you begin to start ignoring them.

So I set out to write something that gave me everything the Spatie package did but also allowed me to throttle how frequently notification of a given type would be sent to us.

Laravel throttled failed jobs!

Continue reading

Today at work we switched away from Telegram to using MS Teams for our company messenger.

Since we we’re already paying for it it made sense to plus it offers a lot more than just a messenger such as custom wiki functionality.

One of the things we were making use of was telegram bots for our applications to sent us updates on key events of interest.

I’ve only recently started to make use of Laravels notifications in the projects I’ve been working on and to send messages via telegram I’d been using this package.

However whilst I’d found documentation that said that sending messages to MS Teams was possible there didn’t seem to be a notification channel for laravel.

So I went about building a basic one today:

Laravel Notifications for Microsoft Teams

Continue reading

Last year I began working at a new startup - NX Technology.

As a start up with limited funding we’re constantly looking for efficiencies and ways to get the most out of what we already have whether that be in hardware or software.

Whilst previously I’ve set up and self hosted a gitlab instance at NX we needed to get things up and running asap so we decided to use the gitlabs free tier.

At the time of writing the bronze tier comes with unlimited private repositories and 2000 gitlab ci minutes using gitlab’s shared runners.

One of the things I love about this is that once those minutes are up you can either pay for more or use your own runners to process jobs for your projects.

We have an old -nothing special- server which we’ve set up a gitlab runner on that will handle jobs for us along with gitlab’s shared runners and when our free minutes run out all jobs will run through that server.

Since most of the time when you’re pushing code to gitlab your laptop will be on, you can use your development laptop to help out too!

Continue reading

Recently I blogged about replacing Moment.js with a lightweight alternative Day.js.

In Laravel, when working with dates I’ve been used to using the Carbon method diffForHumans.

It gives us a date value relative to an optional given date, but it defaults to now. Examples of output from the diffForHumans method looks like:

1
2
3
- 5 minutes ago
- One week ago
- Last Year

Rather than:

1
2
3
2019-05-19 18:08:40
2019-05-12 18:13:40
2018-05-19 18:13:40

We can do the same with Moment.js using the fromNow method. Given that Day.js is supposed to have the same API as Moment.js I assumed that the fromNow method would “Just work”.

Turns out that it didn’t. But with some small changes to our code we can get the same fromNow method working with Day.js.

Continue reading

I’m always looking for easy wins to optimise my development workflow and improve the end user experience, so figured after hearing about Purgecss I’d look into it and see how I could integrate it into my work flow.

“Purgecss is a tool to remove unused CSS”

With frameworks like Bootstrap and Zurb providing so many CSS classes that often don’t get used, this looked like it really would be an easy win situation!

Continue reading

The project im currently working on makes use of Spaties Event Projector package. Recently the team at Spatie upgraded the package to Version 2.

One of the changes that was made was to separate all storable events from regular events by moving them from the App\Events to a new App\StorableEvents folder. In moving the new stored events a new namespace of App\StorableEvents was created.

Whilst this isn’t a mandatory change, all new events classes created get in this new namespace and for the sake of keeping code organised, I wanted to move all of our other storable events to this new folder.
Light work until I wanted to replay my events. What I’d forgotten is that the fully qualified class names are stored on the stored_events table that the package uses.

No big deal I thought - ill just search for App\Events\% and replace with App\StorableEvents\%, how hard could that be in SQL? Turns out harder than I thought but not impossible…

Continue reading

When doing any sort of development work I’ve always preferred working on an Ubuntu machine.
Doing so has helped me understand the servers that our production code has always been run on.

Towards the end of last year I started work on a new project that made heavy use of Spatie’s Event Projector package.
Without getting into the package too much, at its core it stores the payloads of all recorded events in a JSON column within a stored_events table.

I hadn’t needed to use JSON columns before and since they were a part of MySQL 5.7 which was released in 2015 I had assumed there’d be no issues with using them.

For the most part I was right, writing unit tests with Laravel’s build in tools has made me come to love writing tests and made TDD part of my daily workflow.
However when I tried to run the same tests within Gitlabs CI/CD pipelines I saw the following:

1
2
3
4
5
6
7
PHPUnit 7.5.6 by Sebastian Bergmann and contributors.

Runtime: PHP 7.2.15
Configuration: /builds/secret-project/phpunit.xml

==> Tests\Unit\ExampleTest ✓
"{"errors":["SQLSTATE[HY000]: General error: 1 no such function: json_extract (SQL: select * from \"stored_events\" where \"event_class\" in (App\\Events\\ClaimReported, App\\Events\\ClaimUpdatedByApp, App\\Events\\ClaimCreated, App\\Events\\ClaimUpdated, App\\Events\\ClaimUploadCompleted) and \"id\" < 1 and json_extract(\"event_properties\", '$.\"claimUuid\"') = 0dab4335-c5e8-3564-9a0a-d0537cd697f4 order by \"id\" desc limit 1)"]}"
Continue reading
  • page 1 of 1
Author's picture

Talv Bansal

Full Stack Developer, Part Time Photographer


Head of Software Engineering


Remote