Colin DeCarlo presented a talk at Laracon Online where among other useful tips, he demonstrated how to shim MySQL functions in an SQLite database (e.g., add functions that MySQL has but SQLite does not).
Here are two examples that I just needed in a project (FLOOR and DATEDIFF):
We’re using Laravel Socialite with a self-hosted GitLab instance to authenticate users for an internal tool.
Every time the session times out, the OAuth flow redirects the user back to the default dashboard, regardless of what URL the user originally requested.
However, Laravel provides a url.intended session key with the original URL; here’s how I used it, with a fallback to the dashboard URL:
I’ve recently been building an ecommerce app based on Laravel. Partway through development, we added geometry fields to a couple of tables in order to determine distances. I’ve been using this spatial package, so SQLite was not an option for my test suite.
As soon as I switched the testing database driver from SQLite to MariaDB, my tests immediately took an extra 12–13 seconds to run, regardless of whether I ran the entire test suite, a single file, or just one test.
This significantly lengthened the feedback loop when making changes to code and re-running tests.
So when I saw Jack Ellis mention that he uses MySQL for his test suite, it made me curious if he had the same issue.
He said that one of his test files runs 39 tests in < 2 seconds, so apparently it’s not been a problem for him.
Context
I’m using the LazilyRefreshDatabase trait added in Laravel 8.62.0 on my entire test suite
Many of my tables have constrained foreign keys referencing other tables
Comparisons
I decided to do some digging; here are comparisons using four different platforms for the same test in my application.
MariaDB
I’ve been using MariaDB as the main database platform on my development machine for years. Currently I’m on version 10.6.4.
A single test took 14s to run
An entire file with 16 tests took 19s to run
In-Memory SQLite Database
I temporarily disabled the geometry features and tried the in-memory SQLite database (DB_CONNECTION=:memory:); it performed much better for the same tests:
A single test too <1s to run
An entire file with 16 tests took ~7s to run
SQLite File Database
I then tried with an SQLite file (DB_CONNECTION=sqlite), and it performed about the same:
A single test took ~1s to run
An entire file with 16 tests took ~7s to run
MySQL 8
I have an installation of MySQL 8 set up for one app that uses some specific MySQL 8 and I figured why not give that a try too.
Here are the results:
A single test took ~1.5s to run
An entire file with 16 tests took ~6.5s to run
Summary
For some reason, MariaDB takes approximately 12–13 seconds to tear down and recreate the database before starting to run tests, but MySQL is much faster.
While testing MariaDB, I opened the raw data directory for the database, and noticed chunks of files being removed and recreated at a time, so perhaps the foreign key constraints are (part of) the culprit here.
I do have 77 databases with ~3800 tables in my MariaDB installation built up from various projects over the years. It seems unlikely, but theoretically possible, that the server size could be part of the problem too.
I think I’ll experiment with switching back to MySQL as my development platform of choice.
Have you run into this same issue? Have any tips or tricks? Let me know in the comments.
I build an oEmbed provider in a Laravel application the other day and needed to parse an arbitrary URL to determine the route and parameters passed in order to determine the response.
Since I already had the routes built for the possible URLs, I didn’t want to duplicate code and re-parse them.
Here’s how I ended up retrieving the route and parameters:
I was trying to export a Trello board to a spreadsheet and include all the cards and checklists but couldn’t find a good way to do that, so I wrote one!
Originally this project was based on Laravel Zero, a command-line Laravel framework. It accepted a JSON file exported from Trello and exports an Excel spreadsheet:
A board becomes an Excel file
Each list becomes a worksheet
Each card becomes a row in the worksheet
Checklist items become individual rows with their name, completion status, and due date
It still does that, but it’s also available now as a webapp for anybody to use simply by logging in with Trello!
Or if for some reason you need to rewrite all files to the live site instead of just those that are missing, add this in the location / { block, ideally as the first line:
rewrite ^/wp-content/uploads/(.*)$ https://{live site domain}/wp-content/uploads/$1;
Apache
Add this line to the .htaccess file:
RewriteRule ^wp-content/uploads/(.*)$ http://{live site domain}/wp-content/uploads/$1 [NC,L]
Notes
If you’re using Laravel Valet: look in ~/.config/valet/Nginx/ for a file with the same name as your site’s domain. If you’re using Laravel Herd: look in ~/Library/Application Support/Herd/config/valet/Nginx/ for a file with the same name as your site’s domain.
I often use Xdebug for troubleshooting and interactively debugging local code as I write it.
Laravel’s artisan command is extremely useful for running code interactively during development. (It’s based on another utility named psysh.)
It can be very useful to set some debug breakpoints and then run code interactively using artisan, but occasionally when I run php artisan tinker, the PHP shell just sits there and doesn’t accept any input until I kill my xdebug listener.