url) ->where('is_active', true) ->get(); if ($activeAccounts->isEmpty()) { throw new RuntimeException('No active platform accounts found for this instance. Please create a platform account first.'); } return DB::transaction(function () use ($name, $platformInstanceId, $languageId, $description, $activeAccounts) { $channel = PlatformChannel::create([ 'platform_instance_id' => $platformInstanceId, 'channel_id' => $name, 'name' => $name, 'display_name' => ucfirst($name), 'description' => $description, 'language_id' => $languageId, 'is_active' => true, ]); // Attach only the first active account — additional accounts can be linked via the channel management UI $channel->platformAccounts()->attach($activeAccounts->first()->id, [ 'is_active' => true, 'priority' => 1, 'created_at' => now(), 'updated_at' => now(), ]); return $channel->load('platformAccounts'); }); } }