mercredi 28 décembre 2016

laravel group by query error

I am trying to get all notification list of a user group by Source_id and type and selecting '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();

but i 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)

Notification Model:

<?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 Notification:

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();
        });

but when i ran same query in phpmyadmin it executed successfully

enter image description here

as error says to include created_at in group by but if i include it then i can't get my desired output as all created_at timestamp is unique



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire