Gabotronix
1 week ago

whereMonth failing to retrieve appointment dates by month

Posted 1 week ago by Gabotronix

Hi everybody, I have two models in my application, Appointment and Date, an appointment has many dates and a date belongs to an appointment, now I want to search for appointments with dates which were created in a specific month, I do this by looking at appointments with dates, where this dates fullDate field is equal to the passed month, I use eloquent whereMonth for this.

Sadly the functions returns no results despite being results, I suspect this might be because Date model fullDate field is a string instead of a timestamp so maybe that's why it's failing, I tried casting fullDate to timestamp but I'm getting the same results.

fullDate is a string like this: "26-3-2020".

This is my controller method:

public function list2(Request $request)
    {
        $month = $request->input('month');

        $appointments = Appointment::whereHas('dates', function ($query) use ($month){
            $query->whereMonth('fullDate', $month);
        })->with(['user'])->latest()->paginate($request->input('paginate'));
		
        
    }

Date model migrations:

public function up()
    {
        Schema::create('dates', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('appointment_id')->index();
            $table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade')->onUpdate('cascade');
            $table->string('fullDate');//34-3-2020
            $table->timestamps();
        });
    }

Any idea why it's failing.

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