Faster MySQL Counting on JOINS

Lets say you want to count how many customers, sales and suppliers/vendors some user in your system has, in a single query. A naive approach would be to do this: SELECT user_accounts.id, user_accounts.company_name, COUNT(DISTINCT suppliers.id) AS suppliers, COUNT(DISTINCT customers.id) AS…

Write Laravel Tests to Assert Query Count

Want to write a Laravel test to assert that a request or action is not running too many DB queries? This can be useful to ensure that some change won't bombard your database for larger datasets. The reason that I've set it up, is that recently we discovered that one…

Detect Language / Unicode Scripts in PHP

I've created packages that depending on knowing the text script/language to decide which font to use. For that I've created php-string-script-language which will do that exactly. You include it in your project and use it as below: use LasseRafn\StringScript; StringScript::isThai('Hello world.'); // false StringScript::isChinese('你好世界。…

User Initial Avatars

I recently published an article about generating initial avatars in PHP. In extension of that, I created a public and free API that does this for you. It has most of the settings, fast response and is open source. In the coming weeks, I will be tweaking and optimizing the…

Localized Dates In Laravel and Carbon

By adding one single line of code to any of your service providers in Laravel, your $user->created_at->diffForHumans() will be localized. Code to add: Carbon::setLocale(app()->getLocale()); Example: <?php namespace App\Providers; use Carbon\Carbon; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends…

PHP Ordrestyring v2 API Client Library

This one has been gathering dust in my private repositories for some time, so I decided to make it publicly available. It's a simple client library used to connect to Ordrestyring. I'm continously adding more supported endpoints, tests and methods as I find time or need them internally for our…

Advanced Route Model Binding in Laravel

So, imagine you have a platform on which each user has a list of Invoices, and those invoices have a number starting from 1000 for each user. You might still want to use URLs and endpoints like: /invoices/1005 instead of the primary key in the database. So, naively you…

Generating Initials from Names with PHP Initials Package

For a lot of projects, we use initials (Lasse Rafn = LR or LRA) to display employees and users. Repeating code over and over eventually felt wrong, so I packed the primary functionality into two different packages. Prepare yourself for some shameless self-promotion... okay, I do feel a bit shame. 1.…

Making Classes Iterable with ArrayAccess in PHP

I'm writing quite a lot API wrappers in PHP, and most GET index responses are paginated, so usually I return a PaginatedResponse class to the user of the wrapper, that has some different properties such as: items, page, totalPages and so on. However, for responses that are not paginated, I…

Faster Tests Using Stub Database Files

While building a larger application that has on a lot of feature tests which depends on data from the database, I found myself waiting upwards 10 minutes for my test-suite to finish, mostly due to migrations. Surely this is not an issue, and I've seen worse, but what if I…