PHP

These posts are about a subject that involves PHP.

November 21, 2023

The Differences Between UpdateOrCreate And UpdateOrInsert in Laravel

When you want to update a record and create it when it doesn't exist, you can use the updateOrCreate or updateOrInsert method in Laravel. The so-called "upsert" methods....

Read

October 25, 2023

Mock Request Dependency in Laravel

In Laravel it is seen as good practice to dependency inject the Illuminuate\Http\Request object in your methods when possible, instead of using the request() helper. One of...

Read

October 24, 2023

Replace Multiple Items From String Text in PHP With Array

In this post I just want to show a very small trick I came across recently. You probably know the internal PHP function str_replace. When looking at the documentation, we can see...

Read

October 20, 2023

Using and Converting the WebP Image Format in your Laravel Project

WebP is a modern image format for the web that is developed by Google. It is based on the VP8 video format, and provides superior compression. Because of these qualities, it is intended to replace...

Read

September 12, 2022

How To Use Dataproviders When Testing Request Validators In Laravel

When writing request validators in Laravel, it is useful to know which incoming data will lead to a validation error, and which data will pass. A convenient way to test this, is to write unit...

Read

November 10, 2021

Refactoring Multiple If Statements to Objects

In this article I would like to describe how you can refactor a method that contains multiple if-statements by creating objects with a single responsibility, each making their own assertions. Most...

Read

October 26, 2021

Database Transactions in Laravel with saveOrFail, updateOrFail, and deleteOrFail

When you want to rollback a set of database operations if one of them fails, you can execute them within a database transaction. Laravel provides several techniques to do this. One of them, that I...

Read

May 25, 2021

How to Remove Null Values From a Collection in Laravel

Sometimes, you may end up with a collection that contains null values. Removing those null values from the collection can be done very smoothly with the filter...

Read

May 14, 2021

Sending Laravel Notifications Without Notifiable

Notifications in Laravel are typically used to notify users in your application. Users do not have to be the App\Models\User model, but can be any model that includes the...

Read

February 11, 2021

Six Ways to Get Raw SQL Output From Query Builder in Laravel

When working with Laravel, you will probably use the query builder in order to create and run database queries. Sometimes you want to see the raw SQL output of a query, for example when you are...

Read

November 25, 2020

How to Split a Collection in Laravel 8

Since Laravel 8.16, it is possible to split a collection into chunks in two different ways. The first one, that already existed, is the split method: This method breaks a collection into a...

Read

November 16, 2020

Excluding Routes From a Catch-All Route by Using Regex in Laravel

When creating a catch-all route in Laravel, you might encounter conflicts with others routes in your application. In this post I would like to describe how you can resolve some of these conflicts....

Read

October 1, 2020

Practical Options for the Route List Command in Laravel

When you want to show a list of all registered routes in your application, you can use the php artisan route:list command. The output of this command can be a bit confusing when you are...

Read

September 1, 2020

How To Get The Output Of An Artisan Command When Called Programmatically?

Sometimes you want to call an Artisan command from your code. Some commands show data. How do you read the data that an Artisan command may show in the console? For example, you could have a...

Read

August 18, 2020

What Are The Differences Between Update And Fill in Laravel?

When you need to update a resource in Laravel, you might decide to use the update or the fill method. Let's take a look at the source code of the fill and...

Read

July 8, 2020

What is Value Object Casting in Laravel?

Laravel 7 introduced Custom Eloquent Casts. Next to primitive types, such as json, it is also possible to cast values that form a compound to objects. This is an effective way to introduce value...

Read

June 17, 2020

Configuration Caching Caveats in Laravel

With configuration caching you can bundle all configuration files, to be loaded faster by Laravel. When you run php artisan config:cache, a new file will be created in...

Read

May 16, 2020

A New Syntax For Foreign Keys in Laravel 7

Since 7.x, Laravel provides a very compact syntax for creating foreign key constraints with the constrained method. In this post we will give a short demonstration of this syntax, and we...

Read

April 24, 2020

Refactoring Nested Queries in Laravel With Dot Notation And Arrow Functions

Recently I stumbled upon a nested query in a Laravel project. It was written like this: You might have seen a comparable nested query in the code you had to work on, and you probably agree that...

Read

April 19, 2020

How to Use Stub Customization in Laravel

Laravel 7 came with a new feature, stub customization. Stub customization gives the ability to modify the stub files that are used when creating classes such as controllers and tests with the...

Read

March 29, 2020

Run Tests With PHP Artisan Test

Since the release of Laravel 7.x, you can run you tests by executing as an alternative to running your tests with the phpunit command. This command will run all your tests, until the...

Read

March 11, 2020

How to Use Fluent String Operations in Laravel 7

Laravel 7 has an excellent new feature, called fluent string operations. It provides an "object-oriented interface for working with string values", e.g. becomes In this post I would like to...

Read

February 27, 2020

How to Ignore PHPCS Warnings

PHP_CodeSniffer is a useful tool to check your code against a set of standards and rules. It can help in securing a certain coding standard and style within a development team, by running PHPCS as a...

Read

February 21, 2020

Run Elasticsearch With Laravel on MacOS

A guide on how to install Elasticsearch on MacOS in order to use it as a driver for Laravel Scout

Read

February 1, 2020

Binding API Connection Interfaces To Implementations in The Laravel Container

The Laravel service container is called "a powerful tool for managing class dependencies and performing dependency injection". In this post, we will look at a possible use case of the service...

Read

January 23, 2020

Passing Multiple Arguments To Laravel Policy Methods

In most cases, when writing policies in Laravel, you only pass one argument to a policy method, since the authenticated user is already available through dependency injection and no other resources...

Read

December 20, 2019

Using The Where Clause in Validation Rules

Just recently I discovered you can apply where statements to the unique and exists validation rules in Laravel, without using a closure. This means you can...

Read

December 15, 2019

Validating Array Lengths in Laravel

One of the cool things of Laravel's validation rules, is that you can apply certain rules to different data types. When checking the minimum and maximum size of an array, you can use the...

Read

Sign up for the newsletter