-1

I have many to many relationship between user and days. A day can have many users, a user can have many gifts. Is there any way to make this day_id unique with the users?

Example of table shown below:

**user_id  day_id**
    1       1
    1       1     cannot get prize in day 1 if he already got one
    2       1
    3       1
    1       2
    2       2
    2       2     {not again user_id 2 can get day_id 2 gift}
  • Does this answer your question? [How I can put composite keys in models in Laravel 5?](https://stackoverflow.com/questions/31415213/how-i-can-put-composite-keys-in-models-in-laravel-5) – aimme Nov 16 '21 at 08:02

2 Answers2

1

I actually solved this by simply adding a validation rule: 'day_id' => Rule::unique('user_day', 'day_id')->where(function ($query) { return $query->where('user_id', $this->user_id); }), 'user_id' => ['required', 'exists:users,id']

0

Probably you could define a "role" column in users table to divide user and particular user, then use count() method to do the limitation in your controller.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 16 '21 at 09:29