简体   繁体   English

如何使用Carbon日期时间扩展名通过在Laravel中将数据库中的出生日期与当前月份匹配来获取数据?

[英]How to get data by matching month of birthdate from database to current month in laravel using Carbon datetime extension?

I need exactly same results as I have given below in SQL query in Laravel 5. 我需要与下面在Laravel 5中的SQL查询中给出的结果完全相同的结果。

SELECT * FROM `customers` WHERE MONTH(birthdate) = MONTH(NOW())    // get get current month's birthday

In my controller , I have code this. 在我的控制器中 ,我有此代码。

$customer['current_dob'] = Customers::where( DB::raw('MONTH(birthdate)','=','MONTH(NOW())'))->get();

Model: 模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customers extends Model
{
    protected $table = 'customers';
    protected $fillable = array('fname','lname','email','mobile','birthdate','aniversary','gender','amount');
}

A good scenario for using whereRaw 使用whereRaw好方案

// Get customer with birth date in current month
Customers::whereRaw('MONTH(birthdate) = MONTH(NOW())')->get();

// Get customer with birth date in a specific month
$month = Carbon\Carbon::now()->month; // Current month with Carbon
Customers::whereRaw('MONTH(birthdate) = ?', [$month])->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM