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

davy_yg's avatar
Level 27

comparing date and datetime

DashboardController.php

public function searchViewTrxHarian(Request $request) {

        $data1 = date("Y-m-d", strtotime($request->start_date));
        $data2 = date("Y-m-d", strtotime($request->expire_date));

        $trxharianPromo = TrxHarianModel::where(date("Y-m-d",'paydate'), '>', $data1)
                        ->where(date("Y-m-d",'paydate'), '<', $data2)
                        ->get();    

         return view('pages.dashboard.dashboardTrxHarian')
                ->with('list', $Report)
                ->with('date1', $data1)
                ->with('date2', $data2);

        }

Hello,

I think I am getting error when converting the paydate into date only since paydate is date and time.

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from tb_r_solusiorder left join tb_m_biller_solusi on tb_r_solusiorder.productid = tb_m_biller_solusi.Productid and tb_r_solusiorder.billerid = tb_m_biller_solusi.Billerid left join tb_m_biller on tb_r_solusiorder.billerid = tb_m_biller.biller_id where > 2018-08-01 and < 2018-08-31)

Any clue how to fix this error message?

0 likes
2 replies
tykus's avatar

Your where clauses are not using a column name where(date("Y-m-d",'paydate'), '>', $data1), you need a SQL cast - assuming the paydatecolumn is a valid column name on the table:

$trxharianPromo = TrxHarianModel::whereDate('paydate', '>', $data1)
    ->whereDate('paydate', '<', $data2)
    ->get();  
rawilk's avatar

Why not just use Carbon instances?

$data1 = Carbon::parse($request->start_date);
$data2 = Carbon::parse($request->expire_date);

$trxharianPromo = TrxHarianModel::where('paydate', '>', $data1)
    ->where('paydate', '<', $data2)
    ->get();

IMO Carbon is much easier to use than using date all the time.

2 likes

Please or to participate in this conversation.