Laravel 多條件去重

sgm4231發表於2020-10-21
public function deWeight()
    {
        $uniqueAddresses = [];
        $duplicates = [];

        YunUserEntry::orderBy('id', 'desc')->get()
            ->map(function (YunUserEntry $venue) use (&$uniqueAddresses, &$duplicates) {
                $address = sprintf("%s.%s.%s",
                    $venue->company_id,
                    date('Y-m-d', strtotime($venue->interview_date)),
                    $venue->id_no);

                if (in_array($address, $uniqueAddresses)) {
                    // address is a duplicate
                    $duplicates[] = $venue->id;
                } else {
                    $uniqueAddresses[] = $address;
                }
            });

        $res = YunUserEntry::whereIn('id', $duplicates)->delete();

        return BaseController::returnJson(200, "去重{$res}條", null);

    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章