Filtrar dados de um relacionamento N-M Laravel

  Kiến thức lập trình

I need help because I don’t know how to do a search and I don’t know if there’s a way to do it through a query or if the only way is to use a foreach.

I have two tables, a table called ‘leads’, which is a table of users and a table of plays that I named ‘plays’.

The user can have multiple moves and one move belongs to a single user.

In this play table, a column called ‘time’ is his playing time.

On the website there is a ranking list, which lists the users who played that day and who had the shortest playing time. What I need to do is bring all the players, from the same month and day, ordering from the shortest time to the longest. This is already done, the problem now is. I can’t bring the same user twice, I have to filter his best move and show the best move, that is, the move that took the shortest time.

Can anyone help me? Below I currently have the query

Query:

$data = Lead::query();
$data = $data->with('plays', function($query){
        $query->whereMonth('played_at', '=', Carbon::now()->month)->whereDay('played_at', '=', Carbon::now()->day);
});
$data = $data->get();

LEAVE A COMMENT