JSON columns in the database seem like a solution if you're not sure about the structure of data. But quite often, they lead to more problems in the future. In this article, I want to show both good and bad cases of using JSON columns with Laravel examples.
JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn't we? This tutorial will show you an example.
There are situations when we're not sure what the columns of the DB table would be, they need to be flexible. For example, e-shop product properties: size, color, fabric, there may be more in the future. What is the best DB structure? I will show you 3 options and their performance.
How to identify the requests/endpoints that consume the most RAM in your application? Laravel Telescope may help you, with the information in its DB JSON fields.
If you're building a Laravel project with both Web and API sides, you need to customize error messages for each of them separately. In web-view there should be error pages, and API exceptions should return JSON with status codes. How to handle it? I will show you an example with case of Model Not Found 404.
Not sure if you've noticed, but Laravel 5.6 make:auth is generating Blade files with translation-ready texts, so instead of just "Login" you will see {{ __('Login') }}. How does it work and what's the reason?