noblemfd
1 month ago
109
4
Laravel

Email address field is not saving to the database

Posted 1 month ago by noblemfd

I have this code:

public function rules()
{
    return [
        'organization_code'     => 'required|string|min:1|max:20|unique:org_companies',
        'organization_name'     => 'required|string|min:3|max:255|unique:org_companies',
        'org_image'             => 'nullable|image|mimes:jpeg,bmp,png,gif|max:2048',
        'email'                 => 'nullable|email|max:50|unique:org_companies',
        'secondary_email'       => 'nullable|email|max:50|unique:org_companies',
        'total_employees'        => 'nullable|numeric|max:200000',
    ];
}


public function store(StoreCompanyRequest $request)
{
    abort_unless(\Gate::allows('company_create'), 403);
    try {           
            $orgStartDate = Carbon::parse($request->org_start_date);  

            $arr = [
                'organization_code'     => $request->organization_code,
                'organization_name'     => $request->organization_name,
            'org_image'             => 'nullable|image|mimes:jpeg,bmp,png,gif|max:2048',
                'total_employees'       => $request->total_employees,
                'email'                 => $request->email,
                'secondary_email'       => $request->secondary_email,
            ];
            
            
             if ($request->org_image != "") {
                 $org_image = $request->file('org_image');
                 $new_name = rand() . '.' . $org_image->getClientOriginalExtension();
                 $org_image->move(public_path('storage/companies/image'), $new_name);
                 $arr['org_image'] = $new_name;
            }
            Session::flash('success', 'Company is created successfully');
            return redirect()->route('organization.companies.index');                
            
    } catch (Exception $exception) {
            Session::flash('danger', 'Action failed!');
            return redirect()->route('organization.companies.index');  
    }
}

view:

    <form action="{{ route("organization.companies.store") }}" method="post" class="form-horizontal" enctype="multipart/form-data">

        {{csrf_field()}}

            <div class="text-center">
              <input type="image" class="profile-user-img img-fluid img-circle"
                   src="{{asset('theme/adminlte3/dist/img/company_logo.png')}}"

                    id="wizardPicturePreview" title="" width="150">                   

              <input type="file"  name="org_image" id="wizard-picture" class="" hidden>
              <h4 class="profile-username text-center">Click On Image to Add Logo</h4>
            </div>
                <hr class="m-t-0 m-b-40">  
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3"> Company Code<span style="color:red;">*</span></label>
                                    <div class="col-md-9 controls">
                                        <input  type="text" name="organization_code" placeholder="Enter company code here" class="form-control" value="{{old('organization_code')}}">
                                    </div>
                                </div>
                            </div>
                            <!--/span-->
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3"> Company Name<span style="color:red;">*</span></label>
                                    <div class="col-md-9 controls">
                                        <input  type="text" name="organization_name" placeholder="Enter company code here" class="form-control" value="{{old('organization_name')}}">
                                    </div>
                                </div>
                            </div>
                            <!--/span-->
                        </div>
                        <!--/row-->
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3"> Total Employees</label>
                                    <div class="col-md-9 controls">
                                        <input  type="number" name="total_employees" placeholder="Enter total employees here" class="form-control" value="{{old('total_employees')}}">
                                    </div>
                                </div>
                            </div>
                            <!--/span-->
                        </div> 
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3"> Email</label>
                                    <div class="col-md-9 controls">
                                        <input  type="email" name="email" placeholder="Enter Email: [email protected]" class="form-control" value="{{old('email')}}">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3"> Alternative Email</label>
                                    <div class="col-md-9 controls">
                                        <input  type="email" name="secondary_email" placeholder="Enter Alternative Email: [email protected]" class="form-control" value="{{old('secondary_email')}}">
                                    </div>
                                </div>
                            </div>
                            <!--/span-->
                        </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-offset-3 col-md-9">
                                        <button type="submit" class="btn btn-primary">Add Company</button>
                                        <button type="button" onclick="window.location.href='{{route('organization.companies.index')}}'" class="btn btn-default">Cancel</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
    </form>

When I click on save, I observed that every other field was saved into the database apart from

email and secondary_email

I did the var_dump() and observe that they are null.

How do I resolve this?

Please sign in or create an account to participate in this conversation.