在数据库中存储非必填字段

store non mandatory fields in the database

I am trying to insert data into the database with following fields except few which are not mandatory(licensename,licensenumber and file).

signup.blade.php

            var lic = [];
            var licname = [];
            var licnum = [];
            var licfile = [];

            $('#country1').select2();
            $('#sign_up1').click(function () {
                var lic_group = [];
                fname = $("#fname1").val();
                lname = $("#lname1").val();
                country = $('#country1').select2('data').text;
                company = $("#company1").val();
                role = "legal";
                designation1 = $("#designation1").val();
                industry = $("#industry1").val();
                dob = $("#dob1_input").val();
                email = $("#email1").val();
                password = $("#password1").val();
                cnf_password = $("#cnf_password1").val();
                practices = $("#practices").val();
                var data = new FormData();
                data.append("fname", fname);
                data.append("lname", lname);
                data.append("country", country);
                data.append("company", company);
                data.append("role", role);
                data.append("designation", designation1);
                data.append("industry", industry);
                data.append("dob", dob);
                data.append("email", email);
                data.append("password", password);
                data.append("practices", practices);
                var i = 0;
                $("[id^=main_lic_]").each(function () {
                    data.append('licname_' + i, $(this).find('.lic_name').val());
                    data.append('lic_no_' + i, $(this).find('.lic_no').val());
                    data.append('lic_file_' + i, $(this).find('.lic_file')[0].files[0]);
                    i++;
                });
                if (fname == "" || lname == "" || country == "" || company == "" || designation1 == "" || industry == "" || dob == "" || email == "" || password == "" || practices == "") {
                    show_notification("error", "Please enter all required fields");
                } else {
                    if (password != cnf_password) {
                        show_notification("error", "Password and confirm password doesn't match");
                    }
                    else {
                        $.ajax({
                            url: "reg",
                            type: "POST",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: data,
                            success: function (data) {
                                if (data.msg == "Register Successful") {
                                    show_notification("success", "Registered Successfully");
                                    setTimeout(function () {
                                        window.location.href = "/";
                                    }, 1000);
                                }

                            },
                            error: function (result) {
                                show_notification("error", data.responseJSON.message);
                            },
                            contentType: false,
                            processData: false
                        });
                    }
                }

            });

UserController.php

public function registerLawyer(Request $request)
    {
        $firstname = $request->get('fname');
        $lastname = $request->get('lname');
        $country = $request->get('country');
        $company = $request->get('company');
        $role = $request->get('role');
        $desig = $request->get('designation');
        $dob = $request->get('dob');
        $email = $request->get('email');
        $pass = $request->get('password');
        $indus_sec = $request->get('industry');
        $all_params = $request->keys();
        $total_licenses = preg_grep("/^licname_.*/", $all_params);
        if($role == 'legal' && ($request->get('licname_0') == null || $request->get('lic_no_0') == null || $request->file('lic_file_0') == null)){
            abort(403, 'Institute License is mandatory');
        }
        $userWithExistingEmail = User::where('email', $email)->first();
        if ($userWithExistingEmail) {
            abort(403, 'Account already exists with this email');
        }
        if(!in_array($role, ['legal', 'client'])){
            abort(403, 'Invalid role request');
        }

        $user = new User();
        $user->email = $email;
        $user->firstname = $firstname;
        $user->lastname = $lastname;
        $user->country = $country;
        $user->company = $company;
        $user->role = $role;
        $user->designation = $desig;
        $user->dob = Carbon::parse($dob)->toDateString();
        $user->status = ($request->get('role') == 'legal') ? 0 : 1;
        $user->password = bcrypt($pass);
        $user->save();
       /* foreach (explode(',', $request->get('industry')) as $userIndustry){
            $uIndustry = new UserIndustry();
            $uIndustry->user_id = $user->id;
            $uIndustry->industrysector_id = $userIndustry;
            $uIndustry->save();
        }*/
        if($role == 'legal'){
            $lawyer = new Lawyer();
            $lawyer->user_id = $user->id;
            $lawyer->save();
            for ($i = 0; $i < count($total_licenses); $i++) {
                $filename = $request->file('lic_file_0' . $i)->store('licenses');
                $licenseno1 = $request->get('licname_0' . $i);
                $licenseName = $request->get('lic_no_0' . $i);
                $license = new License();
                if($filename != null && $licenseno1 != null && $licenseName != null){
                    $license->lawyer_id = $lawyer->id;
                    $license->file = $filename;
                    $license->institution = $licenseName;
                    $license->licenseno = $licenseno1;
                    $license->save();
                }else{
                    $license->lawyer_id = $lawyer->id;
                    $license->save();
                }

            }
            foreach(explode(',', $request->get('practices')) as $aop){
                $uAop = new UserAOP();
                $uAop->user_id = $user->id;
                $uAop->aop_id = $aop;
                $uAop->save();
            }
        }
        return ['msg' => 'Register Successful'];
    }

在此我想将 $filename、$licenseno1 和 $licenseName 保留为非强制性意味着用户可以将此字段留空我可以在不使此字段为空的情况下实现此目的吗?

不,您不能在不允许 null 的情况下将 null 值存储在数据库中。