EN VI

Php - Laravel using inRandomOrder how to prevent duplicate record?

2024-03-17 15:30:07
Php - Laravel using inRandomOrder how to prevent duplicate record

I'm Using Laravel 10 I'm trying to get Random Record Without Duplication using inRandomOrder But It Giving Duplicate Record Some Time. help me to Solve It

My Controller Code

$Question = Question::inRandomOrder()->limit(50)->get();

Solution:

To get random records without duplication using Laravel's inRandomOrder() method, you can use distinct() method to get unique records

$questions = Question::inRandomOrder()
->limit(50)
->distinct()
->get();
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login