Laravel - Select five users at randon each day

Viewed 143

I have three model classes: Userinfo, Winners, Participants

UserInfo id, user_code, created_at

Winners id, user_code, prize

Participants id, user_code, prize

  1. I want to select 5 user_code at random each day from UserInfo and save the into Winners. prize = winners, user_code will be the user_code from UserInfo

  2. I want to select other 3 user_code at random each day from UserInfo and save the into Winners. prize = participants, user_code will be the user_code from UserInfo. But this three users here will be different from the five users in Winners.

How do I write Laravel code for this? Thanks

1 Answers

You can make a backend job which will repeat every day and use Participants::inRandomOrder()->limit(5)->get() method of eloquent for random users. Check here about daily jobs:- https://laravel.com/docs/5.8/queues

Related