laravel group by query error

时间:2016-12-28 11:59:12

标签: php mysql laravel laravel-5

我想通过Source_id获取用户组的所有通知列表并键入并选择“Source_id”,“type”,“created_at”:

    $notificationlist = Notification::select(['Source_id', 'type','created_at'])
        ->where('user_id', $user->id)
        ->groupby('Source_id','type')
        ->orderby('created_at', 'desc')
        ->get();

但我明白了:

SQLSTATE[42000]: Syntax error or access violation: 1055 'a.notifications.created_at' isn't in GROUP BY 
(SQL: select `Source_id`, `type`, `created_at` from `notifications` where `user_id` = 1 group by `Source_id`, `type` order by `created_at` desc)

通知模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Notification extends Model
{
    protected $fillable = ['activity_type','type','Source_id', 'created_at'];


    public function user()
    {
        return $this->belongsTo('Cartalyst\Sentinel\Users\EloquentUser');
    }
}

架构通知:

Schema::create('notifications', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('Source_id')->unsigned();
            $table->integer('type');
            $table->integer('activity_type');
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table->timestamps();
        });

但是当我在phpmyadmin中运行相同的查询时,它成功执行了

enter image description here

因为错误说包含created_at in group by但如果我包含它然后我无法得到我想要的输出,因为所有created_at时间戳都是唯一的

0 个答案:

没有答案