Laravel Eloquent increment a column based on a relation

Viewed 1036

I currently have a table of image links associate with another model as follows

+----+-------------+----------------+
| id | property_id |      url       |
+----+-------------+----------------+
|  1 |           2 | /example.jpg   |
|  2 |           7 | /example-2.jpg |
|  3 |           5 | /example-3.jpg |
+----+-------------+----------------+

I would like to add a column to the table to order the images as follows

+----+-------------+----------------+------------+
| id | property_id |      url       | sort_order |
+----+-------------+----------------+------------+
|  1 |           2 | /example.jpg   |          1 |
|  2 |           7 | /example-2.jpg |          1 |
|  3 |           2 | /example-3.jpg |          2 |
+----+-------------+----------------+------------+

Is there way to make a database level constraint (i.e. put it in the migration) that the sort_order value auto_increments but is dependent on the propery_id value, such that the sort_order value is essentially an index with each property_id having it's own index?

1 Answers
Related