Rastagar
Rastagar

Reputation: 183

Distinct not working in Laravel

I'm using laravel 5.2 and trying to select distinct records from items table but not working this my query:

$items = DB::table('items')
        ->join('stock_item','stock_item.stm_id','=','items.item_id')
        ->select('stock_item.stm_id','stock_item.item_name','items.id','items.status','items.quantity')
        ->distinct('items.item_id')
        ->where('items.quantity','>', 0)->where('status','0')->get();

any help.

dd($items)'s result .The result snapshot:

enter image description here

Upvotes: 1

Views: 2097

Answers (1)

Bilas Sarker
Bilas Sarker

Reputation: 459

I think you are looking the following solution

$items = DB::table('items')
        ->join('stock_item','stock_item.stm_id','=','items.item_id')
        ->select('stock_item.stm_id','stock_item.item_name','items.id','items.status','items.quantity')
        ->where('items.quantity','>', 0)->where('status','0')
        ->groupBy('items.item_id')->get();

You don't need distinct to get the result which is attached in the questions.

Upvotes: 1

Related Questions