Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Have you tried adding a ->unique() flag to the userId field in your Project migration, so the Exception can be handled when it tries to insert a new record, and there is already one present?

0

relationship defined in the model alone does not actually handle this behavior. you can either check if a project exist before to creating one or add an index as @joshuaajones suggested, which will throw a mysql exception when trying to insert.

0

Dude you forgot the main part the best method is validation. Validate it using Request and use exists rule to achieve this.

Thanks

0

Sign in to participate in this thread!

Eventy

Your banner here too?

nick2price nick2price Joined 20 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.