📜 ⬆️ ⬇️

“Strict mode” and other MySQL settings in Laravel 5.2

If you remember my post. How to disable strict MySQL mode on Laravel Forge (Ubuntu) , you already know that MySQL 5.7 introduced something commonly called “strict mode”, which is actually a combination of new modes. In sum, they force MySQL to process your requests a bit more strictly than before.

In that article, I showed you how to disable it on Ubuntu. But since then, Adam Wathan has added functionality to Laravel, which allows you to determine whether you are using "strict mode". And also allows you to customize what exactly modes you want to use - and all this through the code.

If you can change the configuration of the application instead of setting up the server, without sacrificing performance, I will always choose this option. One problem is less when deploying a new environment. Therefore, I am extremely pleased with this new feature.

')
It is worth noting that you can use this function not only to disable strict mode in 5.7. You can turn it on in 5.6 and this is very far-sighted. Enable strict mode on any application that works with 5.6, so that later it can smoothly go to 5.7. So you will understand in advance, all that can fall.


MySQL 5.7 and "strict" mode.


Before we talk about the feature itself, let's quickly recall what “strict mode” means.

MySQL has “modes”, each of which enables or disables certain behavior. For example, ERROR_FOR_DIVISION_BY_ZERO is a mode that, as you may have guessed, gives an error when you divide by zero in SQL. If this mode is disabled, you just silently get a NULL result.

"Strict Mode" is actually a list of the following settings included in 5.7 by default:

ONLY_FULL_GROUP_BY
STRICT_TRANS_TABLES
NO_ZERO_IN_DATE
NO_ZERO_DATE
ERROR_FOR_DIVISION_BY_ZERO
NO_AUTO_CREATE_USER
NO_ENGINE_SUBSTITUTION

You can learn more about these modes in the MySQL documentation.

Prior to version 5.7, the only mode that was enabled by default was NO_ENGINE_SUBSTITUTION.

Enable / Disable "strict" mode in Laravel 5.2


Thanks to this new feature, three things can now be done in Laravel: Disable "strict mode", returning to behavior <= 5.6; enable "strict mode" by setting it to behavior 5.7; or configure exactly which modes are included.

These parameters live in the configuration / database.php in the connections.mysql section. To begin with, let's look at the on / off "strict" mode.

'connections' => [ 'mysql' => [ //  MySQL 5.6 'strict' => false, //  MySQL 5.7 'strict' => true, ] ] 

Fine tuning modes


But what if you are not satisfied with the default 5.6 and 5.7 modes? Just customize them yourself.

 'connections' => [ 'mysql' => [ //   ,    strict 'modes' => null, //    ,   strict 'modes' => [], //     ,   strict 'modes' => [ 'STRICT_TRANS_TABLES', 'ONLY_FULL_GROUP_BY', ], ] ] 

That's all


Now you have the opportunity to take complete control over which MySQL modes are included in the code, without touching the server configuration.

By default, I would recommend turning on the “strict” mode completely. But there may be some specific cases or old projects, when it may be necessary to fine-tune the modes and now it is not just possible, but very simple!

Source: https://habr.com/ru/post/278629/


All Articles