Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ssquare's avatar

Help needed to convert update query to updateOrCreate

My current code:

$existing = Student::where('name_last', $student['name_last'])
    ->where('name_first', $student['name_first'])
    ->where(function($query) use ($student) {
        if (array_key_exists('phone_preferred', $student) && !empty($student['phone_preferred'])) {
            $query->where('phone_preferred', $student['phone_preferred']);
        }

        if (array_key_exists('email_preferred', $student) && !empty($student['email_preferred'])) {
            $query->where('email_preferred', $student['email_preferred']);
        }

        if (array_key_exists('home_street_address_1', $student) && !empty($student['home_street_address_1'])) {
            $query->where('home_street_address_1', $student['home_street_address_1']);
        }

        if (array_key_exists('mailing_street_address_1', $student) && !empty($student['mailing_street_address_1'])) {
            $query->where('mailing_street_address_1', $student['mailing_street_address_1']);
        }
        
        if (array_key_exists('mailing_address_city', $student) && !empty($student['mailing_address_city'])) {
            $query->where('mailing_address_city', $student['mailing_address_city']);
        }

 })->first();

 if($existing){

        try{
            Student::where('id', $existing->id)
            ->update($student);
            } catch (\Exception $e) {
             $error_encountered = true;
             $error_arr[] = $e->getMessage();
             $error_row_numbers[] = $row_no; 
            }

}

I could convert it to update or create in following ways.

        try{
            Student::updateOrCreatewhere(
                         ['name_first' => $student['name_first], 'name_last' => $student['name_last]],
                        $student
               );
    
            } catch (\Exception $e) {
             $error_encountered = true;
             $error_arr[] = $e->getMessage();
             $error_row_numbers[] = $row_no; 
            }

But, I could not get how to implement that where query functions in this updateOrCreate.

0 likes
8 replies
vajid's avatar

@ssquare you are almost there

just change

Student::updateOrCreate(
        ['name_first' => $student['name_first], 'name_last' => $student['name_last]],
        $student //assuming it is key value pair, where key is db column name
 );
ssquare's avatar

@vajid and what about this part

->where(function($query) use ($student) {
        if (array_key_exists('phone_preferred', $student) && !empty($student['phone_preferred'])) {
            $query->where('phone_preferred', $student['phone_preferred']);
        }

        if (array_key_exists('email_preferred', $student) && !empty($student['email_preferred'])) {
            $query->where('email_preferred', $student['email_preferred']);
        }

        if (array_key_exists('home_street_address_1', $student) && !empty($student['home_street_address_1'])) {
            $query->where('home_street_address_1', $student['home_street_address_1']);
        }

        if (array_key_exists('mailing_street_address_1', $student) && !empty($student['mailing_street_address_1'])) {
            $query->where('mailing_street_address_1', $student['mailing_street_address_1']);
        }
        
        if (array_key_exists('mailing_address_city', $student) && !empty($student['mailing_address_city'])) {
            $query->where('mailing_address_city', $student['mailing_address_city']);
        }
vajid's avatar

@ssquare why not just use phone and email for selecting student both are unique by the way

ssquare's avatar

@vajid the phone and email are not compulsory fields. They might be present or not. So, its necessary to check them if they are present.

vajid's avatar

@ssquare for update part send id in hidden field or in parameter and in controller check if id is present

$id = request()->student_id;

Student::where('name_last', $student['name_last'])
->when($id, function($q){
    $q->where('id', $id);
})
->first();

Student::updateOrCreate(
['id'=>$id ]
....
ssquare's avatar

How am I supposed to get that student_id???? As this is the scenario of update or create. If there was some field something like id. Will it ever be necessary to write such long code?

Snapey's avatar

Make sure $student only contains fields you want an exact match on (by rejecting other data or copying required values to new array, Then

Student::updateOrCreate($student,$newData)

All the fields in $student will be used as where conditions, $newData will be applied.

1 like
ssquare's avatar

@snapey I got the sense but could you suggest me how can I filter and create $student with only fields that are present. You can see my previous logic in question.

Please or to participate in this conversation.