A Perth teenager stabbed five times as he tried to stop his friend from being attacked by a man allegedly armed with a knife has come close to death.

The 17-year-old was with three friends at McDonald's in Baldivis on Monday night around 10.20pm when the group was approached by two men allegedly claiming to be part of a gang known as '6169' - the postcode for the greater area of Rockingham.

A 17-year-old boy came close to death after being stabbed outside McDonald's in Baldivis. Credit:Heather McNeill

Police alleged a brawl broke out between the two groups after a verbal altercation inside the fast-food outlet spilled out onto the streets.

A witness claimed to Nine News Perth the two men attacked her partner outside McDonald's before turning on their 17-year-old friend.