return tr_optional( $user )->name; // Returns the name if exists and null if the user is null
return tr_optional( $user )->getLocation(); // Returns the location if exists and null if the user is null
// Very powerful with model relationships
return tr_optional( $post->author )->name; // Returns null if the post author is deleted
A utility helper function for gracefully calling objects properties and methods, avoiding
Trying to get property of non-object
and similar errors.This is inspired by Laravel optional helper function.