1

I don't want to have dupliactes into my users table. This would be ok:

╔═══╦════════════╦═════════════╦═════════════╗
║   ║ firstname  ║ lastname    ║ dateOfBirth ║
╠═══╬════════════╬═════════════╬═════════════╣
║ 1 ║ John       ║ Mat         ║ 1999-12-01  ║
║ 2 ║ Dave       ║ Bittner     ║ 1950-06-02  ║
║ 3 ║ John       ║ Mat         ║ 1900-11-02  ║
╚═══╩════════════╩═════════════╩═════════════╝

In my Laravel application (UserController), I am checking if the user already exists by calling the firstOrCreate Method (creating a new user):

public function store(UserRequest $request) {

   $user = User::firstOrCreate([
        'firstname'             => request('firstname'),
        'lastname'              => request('lastname'),
        'dateOfBirth'           => request('dateOfBirth'),
    ]);

    if($user->wasRecentlyCreated) {
        // new
    }else {
        // existing
    }

    return redirect('/users');
}

This worked pretty well. Now, I can create a similar user successful (like the entry with the id=3 in the example table above). If I edit the birthday from entry 3 into 1999-12-01, Laravel says it is OK, but it isn't (duplicate with id=1) !

I also want to check on update, if the entry already exists (duplicate check after every update). I am searching a updateIfNotDuplicate method or something like that (like the firstOrCreate for updates).

This is my update function in the UserController:

public function update(User $user, UserRequest $request) {
    $user->update([
        'firstname'             => request('firstname'),
        'lastname'              => request('lastname'),
        'dateOfBirth'           => request('dateOfBirth'),
    ]);

    return redirect('/users');
}
4
  • I think using updateOrCreate() will fix your problem Commented Dec 30, 2017 at 19:13
  • You have a UserRequest object, why not add a unique constraint to the dateOfBirth column? Commented Dec 30, 2017 at 19:59
  • @Ohgodwhy: It is also possible that two users with the same birthday but a different name exists. I tried to create a MCVE. My real table is a much bigger. You could see it as a hole-table-composite-key (instead of the id field). Commented Dec 30, 2017 at 20:07
  • Maybe this feed can help you https://laracasts.com/discuss/channels/eloquent/how-to-check-for-unique-column-combanation-from-the-validation-rule?page=2 Commented Dec 30, 2017 at 20:18

1 Answer 1

1

You can try this

public function update(User $user, UserRequest $request) {

    $update = [
        'firstname'             => request('firstname'),
        'lastname'              => request('lastname'),
        'dateOfBirth'           => request('dateOfBirth'),
    ];

    // find if your updated user already exists 
    $new_user = $user->firstOrNew($update);

    // Update if the no changes has been made or if no user have been founded
    if($new_user->id == $user->id OR !$new_user->exists){
        $user->update($update);

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You need to change !$new_user->exists() into !$new_user->exists (stackoverflow.com/a/41082793/2056125). After this, is worked pretty well. Thanks a lot for this solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.