0

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}]
1
  • hint: use distinct Commented Feb 25, 2022 at 7:57

3 Answers 3

2

You can use DISTINCT for that purpose

$blog = BlogModel::select('user_id')->distinct()->get();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Try change this:

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

To:

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

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.