簡體   English   中英

如何限制對同一用戶ID添加數據-使用Laravel

[英]How to restrict to add data against same user id - using laravel

我有一些發布數據並針對用戶ID存儲它,我想如果數據已經針對相同用戶ID保存,並且如果我再次針對相同用戶ID發出請求,則不應添加此數據:

我的代碼如下:

     $this->medicalIdentifiers->user_id = $doctorProfile['user_id'];
            $this->medicalIdentifiers->medical_credentials = $doctorProfile['medical_credentials'];
            $this->medicalIdentifiers->registration_number = $doctorProfile['registration_number'];
            $this->medicalIdentifiers->registration_expiration_date = $doctorProfile['registration_expiration_date'];
            $this->medicalIdentifiers->dea_number = $doctorProfile['dea_number'];
            $this->medicalIdentifiers->dea_expiration_date = $doctorProfile['dea_expiration_date'];
            $this->medicalIdentifiers->dea_issue_date = $doctorProfile['dea_issue_date'];
            $this->medicalIdentifiers->npi_number = $doctorProfile['npi_number'];
            $this->medicalIdentifiers->billing_title = $doctorProfile['billing_title'];
            $this->medicalIdentifiers->billing_employment_type = $doctorProfile['billing_employment_type'];
            $this->medicalIdentifiers->other_employment_type = $doctorProfile['other_employment_type'];
            $this->medicalIdentifiers->nadean_number = $doctorProfile['nadean_number'];
            $this->medicalIdentifiers->upin = $doctorProfile['upin'];
            $this->medicalIdentifiers->wcb_authorization = $doctorProfile['wcb_authorization'];
            $this->medicalIdentifiers->wcb_rating_code = $doctorProfile['wcb_rating_code'];
            $this->medicalIdentifiers->wcb_date_of_issue = $doctorProfile['wcb_date_of_issue'];
            $this->medicalIdentifiers->hospital_privileges = $doctorProfile['hospital_privileges'];
            $this->medicalIdentifiers->save();

我在請求中傳遞了user_id,並且我正在使用郵遞員打請求。

提前致謝

您可以在數據庫中的非重復字段中創建唯一索引,或者在保存模型之前,需要檢查對數據庫的查詢,或者您的用戶有此類數據

$medicalIdentifiers = DB::table('youre_table')->where([['user_id', '=', $doctorProfile['user_id']], ['medical_credentials', '=', $doctorProfile['medical_credentials']/*..another field..*/]])->get();

if (!$medicalIdentifiers) {
    //...save
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM