Laravel/PHP Articles

Articles from Laravel/PHP sites on the web

Laravel/PHP Articles
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.

Sign Up For Our Newsletter

Each Week, We curate the top Laravel/PHP articles, jobs, events and More!

We'll send you a weekly email with the latest Laravel/PHP articles, jobs and events. Please review our Code of Conduct in the meantime. Talk to you soon!