Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

birdietorerik's avatar

Unique email in my query

Hi!

Have a query that returns dupicate users

public function getNotificationFlowUsers($golfID)
    {
        $flowusers = User::where('golfclub_id', '=',$golfID)
        ->leftJoin('role_user', 'role_user.user_id', '=', 'users.id')
        ->where('role_user.role_id',10)
        ->orWhere('role_user.role_id',1)
        ->orWhere('role_user.role_id',4)
        ->orWhere('role_user.role_id',6)
        ->get();

         return $flowusers;  
    }

But i want to return only 1 record -> unique value email

Tryed this:

public function getNotificationFlowUsers($golfID)
    {
        $flowusers = User::where('golfclub_id', '=',$golfID)
        ->leftJoin('role_user', 'role_user.user_id', '=', 'users.id')
        ->where('role_user.role_id',10)
        ->orWhere('role_user.role_id',1)
        ->orWhere('role_user.role_id',4)
        ->orWhere('role_user.role_id',6)
        ->unique('users.email')
        ->get();

         return $flowusers;  
    }

But this gives me error:

"message": "Call to undefined method Illuminate\Database\Eloquent\Builder::unique()",
"exception": "BadMethodCallException",
"file":

How can i do this ?

0 likes
3 replies
automica's avatar
automica
Best Answer
Level 54

But i want to return only 1 record -> unique value email

does that mean you want one result per email address or 1 result?

if its the former, you should groupBy

https://laravel.com/docs/10.x/queries#grouping

BTW this:

    ->where('role_user.role_id',10)
        ->orWhere('role_user.role_id',1)
        ->orWhere('role_user.role_id',4)
        ->orWhere('role_user.role_id',6)

can be done by using whereIn:

    ->whereIn('role_user.role_id',[1,4,6,10])
krisi_gjika's avatar

btw, your where('golfclub_id', '=',$golfID) does not work like you expect since you have "orWhere" conditions in the same scope

Please or to participate in this conversation.