I have a few things I use
Before now I have taken a few bricks out and fitted a breeze block - works well when you are dealing with really crumbly old walls that a good kick would bring down. you can then use a multi tool to cut the breeze block.
Otherwise as others have said, some dry wall adhesive mixed works well. I haven't added cement to it in the past, but have added some fast setting contact adhesive! stuff dries like concrete and can take any drilling.
I sometimes drill additional holes in the backboxes to get holes in behind, works really well when the box sits across 2 bricks/blocks and only one has crumbled. you can then look to get screws on the edge of the other or even go diagonally with longer screws. coupled with some grab adhesive around box, have had no issues.
if you have a cavity behind the bricks which have crumbled, you can sometimes get a piece of timber in there, this will span the back of the hole just cut and with some nice long screws will come tight against the back of the bricks when screwed with the backbox, to get this to work you need to get one or more screws in vertically to the wall, you can then fill around edge with what ever is to hand, once set hard remove the vertical screws if you need to.
My final trick is to use a bit of metal conduit to fix into the back box, assuming your chase is deep enough( make a few wide parts if required) you can then fix the conduit in place with saddles which will hold the back box steady, then get the expanding foam out to spray in through back of the box and around edges. trim back once dry.
it all comes down to each house being different and finding what works for you.