0

Am trying to pick the last record from the MongoDB using Laravel MongoDB package. I have a many transaction ID in the database in which some are duplicate ID due to some reasons. In that scenario, I need to groupby the transaction id, and sort it according to time and get the last one.

example

transcation_id status timestamps
1              success 
2              success
3              failed
4              success
3              failed
5              success
3              success

So when I am querying I need the transaction_id of 3 as success, which is the last status with the time stamp. However, my query gives me 3 as failed.

$query  = MyCollection::whereIn('app_id' , $appId->toArray())
                ->select(['transcation_id',.....,'created_at'])
                ->orderBy('updated_at')
                ->groupBy('transcation_id')
                ->get();

Please correct me where am doing wrong.

1 Answer 1

1

Laravel collections has method last

Model::all() -> last(); // last element 

This is the best way to do it.

For better solutions use this doc

Sign up to request clarification or add additional context in comments.

3 Comments

Does jessenger Laravel MongoDB package supports the last method?
@ArunCode Yes supports to jessenger also
Tried, the method does not seem to work. Call to undefined method Jenssegers\Mongodb\Query\Builder::last()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.