Home > Back-end >  How do I filter my DB query results to not display any duplicate value
How do I filter my DB query results to not display any duplicate value

Time:02-24

I have a query like this:

$blog = BlogModel::select('user_id')->get();

and it return this

[{"user_id":2},{"user_id":3},{"user_id":4},{"user_id":4},{"user_id":6}]

I would like Delete duplicate user_id like this

[{"user_id":2},{"user_id":3},{"user_id":4},{"user_id":6}]

CodePudding user response:

You can use DISTINCT for that purpose

$blog = BlogModel::select('user_id')->distinct()->get();

CodePudding user response:

You can use distinct() to force the query to return distinct results.

Try change this: $blog = BlogModel::select('user_id')->get();

By this: $blog = BlogModel::select('user_id')->distinct()->get();

You can read more here: https://laravel.com/docs/9.x/queries

CodePudding user response:

$blog = BlogModel::selectRaw('distinct user_id')->get();
  • Related