我对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: selectpermission
.id
,object
.name
asobject_name
,
permission
.created_at
,object
.id
asobject_id
from
permission
inner joinobject
onobject
.id
=
permission
.object_id
inner joinaction
onaction
.id
=
permission
.action_id
wherepermission
.person_id
= 1 group by
permission
.object_id
order bypermission
.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输出:
任何的想法?
谢谢