首页 > 解决方案 > 仅对一个查询禁用 MySQL 严格模式 Laravel 5.6

问题描述

我只想为控制器内的一个查询禁用 MySQL 严格模式,而不是整个 laravel 应用程序,

我知道禁用 MySQL 严格模式的风险,所以我不想从 config/database.php 为我的整个应用程序禁用它,

我的整个应用程序中只有一个查询遇到问题,所以我想在我的控制器中禁用它,然后再运行一次该查询!

请帮助我在给定的情况下有什么办法。

标签: phpmysqllaravel-5

解决方案


在运行时更改严格模式

config()->set('database.connections.mysql.strict', false);
\DB::reconnect(); //important as the existing connection if any would be in strict mode
Model::select()->get(); // your query called in non strict mode

//now changing back the strict ON
config()->set('database.connections.mysql.strict', true);
\DB::reconnect();

推荐阅读