Hi!
Im looking for the best way to ensure that my objects are being saved properly
What im doing
$user = new User;
$user->username = 'someusername';
$user->password = 'somepassword';
$user->save();
$profile = new Profile;
$profile->user()->associate($user);
$profile->email = "some@email.com"
$profile->save();
Now lets say that email on profiles tables is unique and there is already a "some@email.com". When i execute this the user is saved but the profile not. So there is inconsistency on the data. How can i solve this?
Thanks for helping!
In case this is only example, you need to do input validation and that solves your problem. (see manual)
In case that this are real tables merge them.
I would use 'transactions'. They were designed just for such cases.
http://laravel.com/docs/5.0/database (chapter 'Database Transactions')
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community