There are different weapons for that role.
Missiles and whatnot.
The lasers here are of greatest utility as anti-incoming weapons. You can't bump off a capital ship with 'em, but they're the best thing for popping flying ordnance and enemy aircraft quickly, in rapid succession, without missing, and without using up any of your own ordnance (see also: THEL). Considering how many rounds it takes, say, a Phalanx gun to shoot down a single shell, a laser like this could probably save a fat wad of cash on ammunition. Put a naval reactor behind that, or even just some good generators, then you can just save all your bullets for the occasional prickly situation in which lasers are inconvenienced by fog or smoke or something.
And, yeah, I admit there are some jobs where you just need a plain old deck-mounted machine gun. They're reliable and they work, and I doubt anybody will come up with any kind of weapon in the next five hundred years as reliable, effective, and logistically straightforward as guns. There'll be better materials, guns with higher rates of fire, guns with lower failure rates, better ammunition, and some amazing new new mechanical engineering, but in the end you'll still have some device containing a barrel, a chamber, something to feed rounds, and something to make them go bang. In exotic cases, maybe the bang will even come from something other than burning powder.