dongyu4908 2019-01-08 08:43
浏览 67
已采纳

如何在updateOrCreate eloquent中添加laravel查询函数列表

I am trying to use updateOrCreate with multiple conditions and this condition can be applied occasionally if corresponding data are available. 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.

$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

  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2019-01-08 20:00
    关注

    You can just add all the attributes you want to check for in the array that you pass to 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”