catalog->linksForUser((string) $user->public_id); $remoteSlugs = collect($remote)->pluck('slug')->filter()->all(); DB::transaction(function () use ($user, $remote, $remoteSlugs): void { foreach ($remote as $row) { $slug = (string) ($row['slug'] ?? ''); if ($slug === '') { continue; } if (ShortLink::query() ->where('slug', $slug) ->where('is_managed_here', true) ->exists()) { continue; } ShortLink::query()->updateOrCreate( ['slug' => $slug], [ 'user_id' => $user->id, 'label' => $row['label'] ?? null, 'destination_url' => (string) ($row['destination_summary'] ?? $row['destination_url'] ?? LadillLink::url($slug)), 'source_app' => (string) ($row['source_app'] ?? 'platform'), 'source_kind' => (string) ($row['source_kind'] ?? 'qr'), 'source_ref' => isset($row['source_ref']) ? (string) $row['source_ref'] : null, 'manage_url' => $row['manage_url'] ?? null, 'is_managed_here' => false, 'is_active' => (bool) ($row['is_active'] ?? true), 'clicks_total' => (int) ($row['clicks_total'] ?? 0), ], ); } if ($remoteSlugs !== []) { ShortLink::query() ->where('user_id', $user->id) ->where('is_managed_here', false) ->whereNotIn('slug', $remoteSlugs) ->delete(); } }); } }