TypeRocket / core

TypeRocket core source files where all the magic lives.
https://typerocket.com
36 stars 21 forks source link

Optional Util #89

Closed mohamed-abdul-fattah closed 4 years ago

mohamed-abdul-fattah commented 4 years ago

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.

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
kevindees commented 4 years ago

Hey @mohamed-abdul-fattah

I have a feature coming soon that addresses this need. It is being ported over from the pro version.

Thanks, Kevin