dimanche 5 août 2018

Limiting the number of relationships to a record with MySQL or Laravel

I am looking for a way to limit the number of items that a Laravel model can have.

Model A can have up to 5 model B's related to it, but no more than 5. So far I have a many to many relationship between A and B but that allows for an unlimited amount of relationships between the two.

Something like this

A1 | B1
A1 | B2
A1 | B3
A1 | B4
A1 | B5

Trying to add A1 | B6 would either return an error from MySQL on insert or from Laravel before attempting the insert.

I was looking for something like MySQL's unique that could be applied to the A column but limiting to 5 instead of 1 occurrence.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire