1

我需要与下面在 Laravel 5 的 SQL 查询中给出的完全相同的结果。

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

在我的控制器中,我有这个代码。

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

模型:

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');
}
4

1 回答 1

2

一个很好的使用场景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();
于 2015-07-24T09:06:58.563 回答