user11408553
user11408553

Reputation:

PHP Month year day selection drop down list to generate date format in middleware laravel

I have 3 fields in birth date, day, month and year, as 3 dropdown list. I want to store them in a single Date_of_birth column in the database.

How can I solve that in middleware?

Attempt

public function dob($request, Closure $next) {
    $dobrequest -> all();
    if (isset($dobrequest['day'] && $dobrequest['month'] &&                $dobrequest['year'])) {
        $dobrequest['dob'] = $dobrequest['day']."/".$dobrequest['month']."/". $dobrequest['year'];            
    }
    $request ->replace($dobrequest);
    return $next($request);
}

register.blade.php

<div class="col-md-6 pull-left" style="padding-left:0px;">
   <font size="3">  <label for="dob">Date of Birth</label><br class="clearfix" />
      <div class="form-group clearfix">
          <label for="dob_month" class="sr-only">Month</label>
                  <select name = "dob" id="m" class="form-control">
                     <option>Month</option>
                      @for ($month = 1; $month <= 12; $month++)                  <option value = "{{ $month }}">{{ $month }}</option>                   @endfor
                    </select>
      </div>
      <div class="form-group">
            <label for="dob_day" id="d" class="sr-only">Day</label>
                  <select name = "dob" class="form-control">
                      <option>Day</option>
                        @for ($day = 1; $day <= 31; $day++)                    <option value = "{{ $day }}">{{ $day }}</option>                    @endfor
                  </select>
      </div>
      <div class="form-group">
            <label for="dob_year" id="y" class="sr-only">Year</label>
                  <select name = "dob" class="form-control">
                       <option>year</option>
                      @for ($year = 1950; $year <= 2052 ; $year++)                  <option value = "{{ $year }}">{{ $year }}</option>                  @endfor
                  </select>
      </div>

    </font>
  </div>

registercontroller.php

protected function create(array $data)
    {

       dd($data);

        return User::create([
            'fname' => $data['fname'],
            'lname' => $data['lname'],
            'email' => $data['email'],
            'zipcode' => $data['zipcode'],
            'password' => Hash::make($data['password']),
            'gender' => $data['gender'],
            'dob' => $data['day'],$data['month'],$data['year'],
            'health' => $data['health'],
        ]); 

    }

model-> user.php

protected $fillable = [
        'fname','lname', 'email','zipcode', 'password','gender','dob','health',
    ];

#migration

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('fname');
            $table->string('lname');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('zipcode');
            $table->string('password');
            $table->string('gender');
            $table->DATE('dob');
            $table->string('health');
            $table->rememberToken();
            $table->timestamps();
        });
    }



    ];

Upvotes: 1

Views: 2098

Answers (2)

user4472156
user4472156

Reputation:

You can create wrap function with params day and year string and define function like:

function wrapDate($day, $month, $year) {
    return sprintf("%d/%d/%d", $day, $month, $year);
}

Upvotes: 0

Pintu Kumar
Pintu Kumar

Reputation: 421

You can create helper class for format date, day and year string to date format and define function like:

public function formatDate($year, $month, $day) {
   return strftime("%F", strtotime($year."-".$month."-".$day));
}

Upvotes: 1

Related Questions