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)
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)