首页 > 解决方案 > 如何在 laravel 中的 updateOrCreate 上按月和年过滤?

问题描述

如何created_at在 Laravel 中过滤月份和年份updateOrCreate()

    $statistik = Statistik::updateOrCreate(
        [
            'kode_kantor' => $map->kode_kantor,
            'created_at'  => whereYear('created_at', '=', date('Y-m-d')),
            'created_at'  => whereMonth('created_at', '=', date('Y-m-d'))
        ]
    )->increment('poin_kunjungan');

标签: phplaraveleloquentlaravel-5.7

解决方案


您正在寻找的东西不能由updateOrCreate(). 您将不得不进行手动检查。例如:

$statistik = Statistik::where('kode_kantor', $map->kode_kantor)
    ->whereYear('created_at', '=', date('Y-m-d'))
    ->whereMonth('created_at', '=', date('Y-m-d'))
    ->first(); 

if(! $statistik) {
    $statistik = Statistik::create([
        'kode_kantor' => $map->kode_kantor
    ]); 
}

$statistik->increment('poin_kunjungan'); 

推荐阅读