如何在varchar列中使用Laravel whereYearYear?

时间:2019-04-18 08:33:03

标签: php laravel laravel-5.7

我想使用laravel function isPermitted(req, res, next) { var permitted = false; // Your validation here, is your user permitted with this access or not. if (permitted) { next(); } else { res.send('Sorry, you are not belong here.'); } } /* GET home page. */ router.get('/', isPermitted, function(req, res, next) { //console.log("pending data"); res.locals.connection.query('SELECT id,name,email,username,address,phone,status FROM user', function (error, results, fields) { if (error) throw error; res.send(JSON.stringify(results)); }); }); 在名为schedule的varchar列中过滤年份。但是,它返回一个错误,如下图所示。还有一件事是,我没有将每个人都在使用的数据库中的whereYear()指向whereYear日期列。下面是我的代码。请帮忙。谢谢。

控制器:

created_at

数据库中的列$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)); }

enter image description here

错误:

enter image description here

使用Akash Kumar Verma的答案时出错 enter image description here

1 个答案:

答案 0 :(得分:0)

使用Y-m-d H:i:s时间戳格式的年份

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

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