简体   繁体   English

如何在updateOrCreate雄辩中添加laravel查询功能列表

[英]How to add laravel query function lists in updateOrCreate eloquent

I am trying to use updateOrCreate with multiple conditions and this condition can be applied occasionally if corresponding data are available. 我试图将updateOrCreate与多个条件一起使用,并且如果有相应的数据可用,则可以偶尔应用此条件。 I am currently doing as first find the id if exists and if id exists update else create manually but now i want to use laravel given updateOrCreate function. 我目前正在做的事情是先找到id是否存在,如果id存在,则更新,否则手动创建,但是现在我想使用laravel给定的updateOrCreate函数。

$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 want to implement with something like: 我想用类似的东西来实现:

try{
        Student::updateOrCreate(
                     ['name_first' => $student['name_first], 'name_last' => $student['name_last]], 
                    $student
           ); //I could not get how to implement other occasional where condition

        } catch (\Exception $e) {
         $error_encountered = true;
         $error_arr[] = $e->getMessage();
         $error_row_numbers[] = $row_no; 
        }

I want to include those occasional where function query to updateOrCreate method which is currently implementing in manual method 我想包括那些偶尔在哪里查询功能的updateOrCreate方法,目前正在手动方法中实现

You can just add all the attributes you want to check for in the array that you pass to updateOrCreate 您可以仅将要检查的所有属性添加到传递给updateOrCreate的数组中

// these are your required conditions
$conditions = [
    'name_first' => $student['name_first'],
    'name_last' => $student['name_last'],
];

// these are optional
$attributes = [
    'phone_preferred', 
    'email_preferred', 
    'home_street_address_1', 
    'mailing_street_address_1', 
    'mailing_address_city'
];

foreach($attributes as $attr){
    // check to see if the attribute exists on the student
    // and is not empty
    if(isset($student[$attr]) && !empty($student[$attr])){
        $conditions[$attr] = $student[$attr];
    }
}

Student::updateOrCreate($conditions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM