How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?

前端 未结 12 1735
不思量自难忘°
不思量自难忘° 2020-11-29 11:09

My laravel eloquent is like this :

$products = Product::where(\'status\', 1)
            ->where(\'stock\', \'>\', 0)
            ->where(\'category         


        
12条回答
  •  感情败类
    2020-11-29 11:37

    What I did as a workaround and to prevent further security issues I make it happen like this:

     public function getLatestModels (){
            \DB::statement("SET SQL_MODE=''");
            $latestInserted = Glasses::with('store.deliveryType','glassesHasTags','glassesHasColors','glassesHasSizes','glassesHasImages','glassesBrand','glassesMaterial')->whereRaw("store_id in (select distinct store_id from glasses)")->groupBy('store_id')->orderBy('created_at')->take(8)->get();
            \DB::statement("SET SQL_MODE=only_full_group_by");
    
            return $latestInserted;
        }
    

    this is a kind of combination of other answers. Also if you are using "use Illuminate\Support\Facades\DB;" you don't need backslashes in those DB statements above.

    The only disadvantage here is that we are making three calls to db :(

    p.s. As I see @Felipe Pena answer I guess the second statement is unnecessary

提交回复
热议问题