php – Eloquent group在Laravel 5.3中使用有效的SQL查询生成“SQLSTATE [42000]”

前端之家收集整理的这篇文章主要介绍了php – Eloquent group在Laravel 5.3中使用有效的SQL查询生成“SQLSTATE [42000]”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对Eloquent有一个奇怪的问题,我正在尝试执行以下操作:
$this->node = \DB::table('permission')
                ->select('permission.id','object.name as object_name','permission.created_at','object.id as object_id')
                ->join('object','object.id','=','permission.object_id')
                ->join('action','action.id','permission.action_id')
                ->where('permission.person_id',$this->person['id'])
                ->groupBy('permission.object_id')
                ->orderBy('permission.created_at','desc')
                ->paginate(5);

Laravel Framework报告错误

QueryException in Connection.PHP line 761: sqlSTATE[42000]: Syntax
error or access violation: 1055 ‘permission.id’ isn’t in GROUP
BY (sql: select permission.id,object.name as object_name,
permission.created_at,object.id as object_id from
permission inner join object on object.id =
permission.object_id inner join action on action.id =
permission.action_id where permission.person_id = 1 group by
permission.object_id order by permission.created_at desc limit
5 offset 0)

我在AppServiceProvider中添加了一个Eloquent调试函数DB :: listen:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
         DB::listen(function ($query) {

            echo "<pre>";
            print_r($query->sql);
            echo "</pre>";

            // $query->sql
            // $query->bindings
            // $query->time
        });
    }
    ...

它确实打印了这个SQL查询

select  `permission`.`id`,`object`.`name` as `object_name`,`permission`.`created_at`,`object`.`id` as `object_id` 
from `permission` 
inner join `object` on `object`.`id` = `permission`.`object_id` 
inner join `action` on `action`.`id` = `permission`.`action_id` 
where `permission`.`person_id` = 1 
group by `permission`.`object_id` 
order by `permission`.`created_at` desc 
limit 5 offset 0

这在MysqL中通过PHPMyAdmin有效,这里是查询输出

即便如此,我直接在MysqL命令中测试它确实工作得很好,看看MysqL输出

任何的想法?

谢谢

与laravel 5.3面临同样的问题
他们正在尝试使用MysqL-5.7强制执行严格的查询编写

但是要禁用它,只需转到config / database.PHP并更改strict flag

'MysqL' => [
            .
            .
            .
            'strict' => false,//'strict' => true,.
            .
        ],

希望这也能解决你的问题.

猜你在找的Laravel相关文章