IntroductionHere are some features from Laravel that you may not be aware of.
Here are a few examples: public function user ( ) { return $this -> belongsTo ( 'App\User' ) -> withDefault ( ) ; } //or public function user ( ) { return $this -> belongsTo ( 'App\User' ) -> withDefault ( [ 'name' => 'Guest Author' , ] ) ; } // or public function user ( ) { return $this -> belongsTo ( 'App\User' ) -> withDefault ( function ( $user ) { $user -> name = 'Guest Author' ; } ) ; } For the implenetation details check out SupportsDefaultModels.php .
View composers will bound data to a view every time that view file is rendered.
You can also decide to append data at run time like this: return $user -> append ( 'is_admin' ) -> toArray ( ) ; // or return $user -> setAppends ( [ 'is_admin' ] ) -> toArray ( ) ; See more here.
Please let me know in the comments if there are any useful features of Laravel that you think are underused, that I've not mentioned.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners