douna1892 2019-04-18 08:33
浏览 311

如何在varchar列中使用Laravel whereYear?

I want to filter the year in my varchar column named schedule using laravel whereYear(). However it returns an error shown in the image below. One more thing is that, I am not pointing the whereYear to created_at date column in the database that everyone is using. Below is my code. Please help. Thanks.

Controller:

$year = $request['bill_year'];
$month = $request['bill_month'];
$period = $request['bill_period'];

if( is_numeric($year) ){

      $amortizations = Amortization::whereYear('schedule', $year)->where('bill_status',0)->get();

      if($amortizations){
         $notification = "Success";
       } else{
         $notification = "Failed";
       }

       return json_encode(array('notify'=>$notification, 'amortizations' => $amortizations, 'year' => $year, 'month' => $month, 'period' => $period));                                           
}

Column 'schedule' in the database

enter image description here

error:

enter image description here

error using answer by Akash Kumar Verma enter image description here

  • 写回答

1条回答 默认 最新

  • dongneng5383 2019-04-18 08:39
    关注

    WhereYear working with Y-m-d H:i:s timestamp format

    use DB;
    
    $amortizations = Amortization::where( DB::raw('YEAR(schedule)'), $year )->where('bill_status',0)->get();
    

    or

    $amortizations = Amortization::whereRaw('YEAR(schedule) = '.$year)->where('bill_status',0)->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等