Furkan ozturk
Furkan ozturk

Reputation: 722

How to make this in laravel QueryBuilder

I want to take the max of the progress column that is ordered with a distinct column name.

For example;

progress - journey_id

the code I tried to make but I couldn't do more

  $impressions = DB::table('journey_content_impression')
                ->where('user_id', 11)
                ->where('journey_id',12)
                ->select('journey_item_id','progress')->distinct()->get();

Finally, It should give us one row with the max number. Also, other columns of row should be available like this,

$impressions->first()->user_id; //

Upvotes: 0

Views: 36

Answers (2)

Nurbek Boymurodov
Nurbek Boymurodov

Reputation: 542

Maybe it helps you try this

$impressions = DB::table('journey_content_impression')
->where([['user_id', 11],['journey_id',12]])
->select('journey_item_id','progress')->distinct()->get();

Upvotes: 0

Octivia Bluekit
Octivia Bluekit

Reputation: 93

Try this:

$impressions = DB::table('journey_content_impression')
                ->where('user_id',{id})
                ->where('journey_id',{j_id})
                ->groupBy('journey_item_id')
                ->get(['journey_item_id', DB::raw('MAX(progress) as progress')]);

Upvotes: 1

Related Questions