To preserve the leading spaces when inserting a variable, you just have to escape once the spaces with a backslash \ after the insert command i, like this:
$ sed '50i\'"$add" file
public function add($hardware_id = null)
Is not specific to variables, it would be the same if the text was inserted literally.
Is not a POSIX specified behavior, which mandates a escaped literal newline before the text to be inserted.