Helper functions can be extremely useful in your Laravel projects. They can help to simplify your code in your projects in a clean and easy way. Laravel comes with many helper functions out-the-box such as dd(), abort() and session(). But as your projects start to grow, you'll likely find that you'll want to add your own.

In this article, we’re going to look at how we can create and use our own custom PHP helper functions in our Laravel projects.

Creating the Helper Function

To add our own helper functions, we need to start by creating a new PHP file to place them in…


Controllers play a huge role in any MVC (model view controller) based project. They’re effectively the “glue” that takes a user’s request, performs some type of logic, and then returns a response. If you’ve ever worked on any fairly large projects, you’ll notice that you’ll have a lot of controllers and that they can start to get messy quite quickly without you realising. In this article we’re going to look at how we can clean up a bloated controller in Laravel.

The Problem with Bloated Controllers

Bloated controllers can cause several problems for developers. They can:

  1. Make it hard to track down a particular piece…


In software and web development, it’s always important to write code that is maintainable and extendable. The solution that you first create will likely change over time. So, you need to make sure you write your code in a way that doesn’t require a whole rewrite or refactor in the future.

The strategy pattern can be used to improve the extendability of your code and also improve the maintainability over time.

Intended Audience

This post is written for Laravel developers who have an understanding of how interfaces work and how to use them to decouple your code. …


In programming, it’s important to make sure that your code is readable, maintainable, extendable and easily testable. One of the ways that we can improve all of these factors in our code is by using interfaces.

Intended Audience

This article is aimed at developers who have a basic understanding of OOP (object oriented programming) concepts and using inheritance in PHP. If you know how to use inheritance in your PHP code, this article should hopefully be understandable.

What Are Interfaces?

In basic terms, interfaces are just descriptions of what a class should do. …


During my Christmas holidays at the end of 2019, I spent a bit of my free time building a Laravel package called Laravel Exchange Rates. The package is pretty simple and is just a wrapper for the API, with some caching features added for performance. At the time of writing this post, the package has around 43,000 downloads and has started getting around 4,000 downloads per month. It’s not that big in comparison to some other projects, but I’d like to think that I did something right for it to be getting that many downloads.

My main reason for…

How to Force Eager Loading and Prevent N+1 Issues in Laravel


In a past article, I talked about the performance improvements that you can achieve by using “eager loading” in your Laravel database queries. For anyone who hasn’t read it, click here if you want to give it a quick read.

What is Eager Loading?

When you are fetching any models from the database and then doing any type of processing on the model’s relations, it’s important that you use eager loading. Eager loading is super simple using Laravel and basically prevents you from encountering the N+1 problem with your data. This problem is caused by making N+1 queries to the database, where N is…


There may be times when you are working with arrays in your PHP code and need to switch the order of two items. If you’re using an array that uses integers as keys, this can be a pretty straightforward task. However, if you’re trying to swap two items of an associative array, this can be little bit more hands-on. So, in this short post, I’ll show you how you can use macros in Laravel to keep your code looking clean and reusable.

By the end of the post, you should have a brief idea on how to create a macro…


Testing is an integral part to web and software development. It helps to give you the confidence that any code that you have written meets acceptance criteria and also reduces the chances of your code having bugs. In fact, TDD (test driven development), a popular development approach, actually focuses around tests being written before any code is added to the actual app code base.

Intended Audience

This article is aimed at developers who are fairly new to the Laravel world but have a basic understanding of tests. This article won’t cover how to write basic tests (that’s for another post in the…

How to Validate Email Addresses using Laravel Mailbox Layer


Data integrity and validation are important aspects of web development and data collection. It’s always important that we (as web developers) validate data before we store in the database. As an example, imagine that you’re building a form for a new website that visitors can use to subscribe to your new email newsletter. I’m sure that you’d want to make sure that anyone submitting the form was using a valid email address and not trying to add any spam addresses.

Sure, there are things that you could do to verify that email, such as sending out a confirmation email for…


No matter if you’re brand new to Laravel or have been using it for years, there’s always something new to learn. The framework and its ecosystem are constantly evolving and growing to improve the overall developer experience. So, it can be quite important to keep up to date with the latest changes so that you don’t fall behind.

It’s also likely that you’ve run into a coding problem once or twice and needed to reach out to people online for a bit of advice and help. Let’s be honest, as developers we spend quite a lot of time searching online…

Ash Allen

I’m a Laravel web developer based in the UK. I specialise in building websites and systems for small businesses to help them grow and increase their sales.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store