Home > Mobile >  Get like values with groupBy
Get like values with groupBy

Time:07-05

I have a users table with the following records:

enter image description here

From the above records, i want to fetch only users with like names.

My expected result is to get users with like names as seen in the screenshot below:

enter image description here

So, this is what I've tried:

$users = User::groupBy('name')->having(DB::raw('count(*)'), ">", "1")->select('name')->get();

dd($clients);

But this is the result I was getting:

enter image description here

I want to able to get the users with like names.

CodePudding user response:

I do think this should work in your case:

    User::query()
        ->select('name')
        ->having(DB::raw('count(name)'), '>', 2)
        ->groupBy('name')
        ->get();

CodePudding user response:

    User::query()
   ->where('name', 'LIKE', "%{$searchTerm}%") 
   ->orWhere('email', 'LIKE', "%{$searchTerm}%") 
   ->get();

This will return all records that have a name or email that contains the string in $searchTerm.

So in your case, you might need to add a groupBy statement too.

You can find more details in this link

  • Related