L
L
Lucius Mooning2018-02-16 14:05:32
MySQL
Lucius Mooning, 2018-02-16 14:05:32

How to make such a selection using eloquent?

Good afternoon!
How to make such a selection using eloquent ?
Initial data: there is a pivot table that stores changes in record statuses (record_id) by category with the following fields: id, record_id, category_id, old_status_id, updated_status_id, updated_at.
The task is this: to get a row (if any) with the latest status (for example, equal to 2) for a specific record for each category (if it exists in the table).
Each entry has a one-to-many relationship with a pivot table.
I think it should look something like this:

return $query
            ->wherehas('status_repositories', function($q)
            {
                $q->groupBy('category_id')->having(function( $q_nest )
                    {
                        $q_nest->orderBy('updated_at', 'DESC')->take(1);
                    }
                )
                    ->where('updated_status_id', '2');
            });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question