Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TF120's avatar
Level 1

Trying to create a date of birth form that shows any users in the database with that date of birth

Sorry for the titlegore I don't know the best way to explain my problem I'm quite new to Laravel

So I'm trying to create a textbox where you can insert a date of birth, and will check the database to see any user that has that specific date of birth and then will show on screen via a foreachloop.

This is my AppointmentController

function addAppointment()
    {
        $doctors = Doctor::all();
        $search = request('searchdob');
        $users = User::where('role',  '=',  1)
                       ->where('dateofbirth', 'LIKE', '%'.$search.'%')
                       ->get();
        
    
        return view('appointment/addappointmentform',['users'=>$users],['doctors'=>$doctors]);
    }

This is my addappointmentform.blade

@extends('layouts.master')
@section('title', 'Create Appointment')
@section('content')

<form action="{{url('addAppointmentDatabase')}}" method="POST">
{{ csrf_field() }}
<h1>Create Appointment</h1>
<form>
    Insert Patients date of birth
    <input
    type="text"
    name="searchdob"
    id="searchdob"
    placeholder="dd/mm/yyyy"
    onkeyup="
        var v = this.value;
        if (v.match(/^\d{2}$/) !== null) {
            this.value = v + '/';
        } else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
            this.value = v + '/';
        }"
    maxlength="10"
></form>
    
    
    <fieldset>
<legend>Select the Patient</legend> 
@foreach($users as $user)
    <div>
        <label for="dirBtn{{$user->id}}">
        <input id="dirBtn{{$user->id}}" type="radio" name="user" value="{{$user->id}}">
        {{$user->firstname}}
        </label>
    </div>
@endforeach
</fieldset>
0 likes
3 replies
Screenbeetle's avatar

Hi @TF120

So firstly can I just check some basics. In laravel you would normally pass in your request object as a param (and state the visibility of your method) e.g.

public function addAppointment(Request $request)
{
..
    // $search = request('searchdob'); then this line should be
    $search = $request->input('searchdob');
      ..
}

Second what is the field type for dateofbirth? You'll need to validate your date input to match the field type. Assuming it's datetime (0000-00-00 00:00:00) then your current placeholder format (dd/mm/yyyy) wont match I don't think .

You should convert them both to datetime or Carbon then you can make use of ->whereDate which is built into Eloquent

->whereDate('dataofbirth', '=', $search);
 
TF120's avatar
Level 1

Hi @Screenbeetle

The data type i'm using for dateofbirth is varchar, I know it's not ideal but I don't want to mess with it too much

Screenbeetle's avatar

Hi again @TF120

I know it's annoying to have someone give you preachy advice buuuuut ... varchar for a date is not a terribly good idea. Makes doing really simple things - like your above question, simple queries to your table, generating reports, validating etc - annoyingly tricky.

Have you thought about just biting the bullet and converting the field? It's not that tricky with php. You could write a script to loop through your old varchar date field and convert them into a new datetime new field. Something like:

$varchar_dates = DB::table('table')->select('dateofbirth')->get();

foreach($varchar_dates as $varchar_date) {

    $date_format = 'd-m-Y';  //set you datetime format

    $datetime_date = DateTime::createFromFormat($date_format, $varchar_date);

}

have I persuaded you? :-)

Please or to participate in this conversation.