May Sale! All accounts are 40% off this week.

aayush's avatar

Laravel updateOrCreate 'OR' condition check

Currently am performing updateOrCreate with one condition

                    $base_voter_rec = Base_voter::updateOrCreate(
                     ['voter_id' => $voter_arr['voter_id']],
                     $voter_arr
                    );

Now, How can I perform updateOrCreate with 'OR' case similar like passing array.

I wan't something likle: ['name' => $voter_arr['name'], 'phone' => $voter_arr['phone]] OR ['name' => $voter_arr['name'], 'email' => $voter_arr['email]]

                    $base_voter_rec = Base_voter::updateOrCreate(
                     ['name' => $voter_arr['name'], 'phone' => $voter_arr['phone]],
                     $voter_arr
                    );

How can I achieve this?

0 likes
10 replies
Cronix's avatar
//common to both
$exists['name'] => $voter_arr['name'];

// add parameter depending on condition
if (someCondition) {  
    $exists['phone'] => $voter_arr['phone'];
} elseif (otherCondition) {
    $exists['email'] => $voter_arr['email'];
}

$base_voter_rec = Base_voter::updateOrCreate($exists, $voter_arr);
aayush's avatar

Could you suggest what the someCondition might be.

Cronix's avatar

Not really, I don't know what you're doing. I just showed a way to be able to alter it.

Why do you want/need the or?

aayush's avatar

I just want to update data or create data with condition like if any row has same (name+phone) combination or (name+email) combination then update that row else create one. So, at this case you are suggesting to check condition on condition. So, its little bit confusing for me.

Cronix's avatar
Cronix
Best Answer
Level 67

In that case, you can't really do that with updateOrCreate. You'd have to do it manually using parameter grouping in the where clause

Something like

$existing = Base_voter::where('name', $voter_arr['name'])
    ->where(function($query) {
        $query->where('phone', $voter_arr['phone']);
        $query->orWhere('email', $voter_arr['email']);
})->first();

// select * from base_voters where `name` = 'someName' and (`phone` = 'somePhone' or `email` = 'someEmail')

if ($existing) {
    // do an update on $existing
    $existing->fill($voter_arr);
} else {
    // create new one
    Base_voter::create($voter_arr);
}
lostdreamer_nl's avatar

In Cronix' example you can also replace the call to first() with firstOrNew() so it gives you the existing model or an empty new one.

After that you could use:

$existing->fill($voter_arr);
$existing->save();

And not even care about if it's an update or a create.

1 like
aayush's avatar

@cronix

$existing = Base_voter::where('name', $voter_arr['name'])
    ->where(function($query) {
        $query->where('phone', $voter_arr['phone']);  //error on this line undefined $voter_arr, tried by passing function($query,$voter_arr) but with no success
        $query->orWhere('email', $voter_arr['email']);
})->first();
Cronix's avatar

That should be easy to figure out... I missed passing the var to the closure with the use statement...

->where(function($query) use ($voter_arr) {
aayush's avatar

@cronix

$existing = Base_voter::where('name', $voter_arr['name'])
    ->where(function($query) {
        $query->where('phone', $voter_arr['phone']);  //is there any way to igonre this line if $voter_arr['phone] doesnot exist. Same goes for another line of email
        $query->orWhere('email', $voter_arr['email']);
})->first();
Cronix's avatar

Yes, it's just php. You really should know how to do a basic check with in_array() before even using something like laravel...

http://php.net/manual/en/function.in-array.php

if (in_array('phone', $voter_arr)) {
    $query->where('phone', $voter_arr['phone']);
}

etc.

It would be helpful if you listed all of your requirements up front instead of adding them as we go along.

Please or to participate in this conversation.