skip to Main Content

here is my code –

$list = Plot::active()
             ->whereNotNull('user_id')
             ->distinct('user_id')
             ->with('user')
             ->paginate(10);

but here "distinct(‘user_id’)" not working. I want only unique user_id.

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem in this way -

    $list = User::has('plots')->whereHas('plots', function ($query) {
                $query->active();
            })->with('plots')->paginate(10);
    

  2. To get unique data based on user_id, you can use the groupBy() method instead. Here’s how you can modify your code:

    $list = Plot::active()
                 ->whereNotNull('user_id')
                 ->with('user')
                 ->groupBy('user_id')
                 ->paginate(10);
    

    The groupBy() method ensures that the query groups the results by user_id, effectively making them distinct.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search