如何在laravel中的updateOrCreate上按月和年过滤?

时间:2019-02-10 07:55:22

标签: php laravel eloquent laravel-5.7

如何在Laravel的created_at中用月份和年份过滤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');

1 个答案:

答案 0 :(得分:1)

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