Support the ongoing development of Laravel.io →
Database Eloquent

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!

Last updated 2 years ago.
0

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.

0

I would use 'transactions'. They were designed just for such cases.

http://laravel.com/docs/5.0/database (chapter 'Database Transactions')

http://fideloper.com/laravel-database-transactions

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

archer3cl archer3cl Joined 7 Mar 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.

© 2025 Laravel.io - All rights reserved.