如何限制针对同一用户 ID 添加数据 - 使用 Laravel

问题描述 投票:0回答:1

我有一些发布数据并将其存储在用户 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 并使用 Postman 来命中请求。

php laravel authentication
1个回答
0
投票

您可以在数据库中的非重复字段中创建唯一索引,或者在保存模型之前,您需要检查对数据库的查询或您的用户是否有此类数据

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

if (!$medicalIdentifiers) {
    //...save
}
© www.soinside.com 2019 - 2024. All rights reserved.