{"id":11094,"date":"2021-12-02T19:02:50","date_gmt":"2021-12-02T10:02:50","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=11094"},"modified":"2021-12-02T19:02:52","modified_gmt":"2021-12-02T10:02:52","slug":"the-10-best-new-features-in-laravel-8","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/11094","title":{"rendered":"The 10 best new features in Laravel 8"},"content":{"rendered":"\n<p><strong>1. app\/Models Directory<\/strong><\/p>\n\n\n\n<p>The&nbsp;<code><strong>artisan:make model<\/strong><\/code>&nbsp;command will create the model in the app\/Models directory. This feature was decided after Taylor asked people on Twitter how they feel about this.<\/p>\n\n\n\n<p>If you don\u2019t like that, it\u2019s possible to delete the&nbsp;<code>app\/Models<\/code>&nbsp;directory and artisan will create the model file in the&nbsp;<code>app\/<\/code>&nbsp;folder.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>2. New Landing Page<\/strong><\/p>\n\n\n\n<p>Laravel 8 comes with a new landing page for a brand new install. It is now redesigned and It is built using&nbsp;<a href=\"https:\/\/tailwindcss.com\/\">TailwindCSS<\/a>, includes light and dark-mode capabilities, and by default extends links to SaaS products and community sites.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>3. Controllers Routing Namespacing<\/strong><\/p>\n\n\n\n<p>No more double prefix issues! In previous versions of Laravel, the RouteServiceProvider had an attribute called namespace that was used to prefix the controllers in the routes files. That created a problem when you were trying to use a callable syntax on your controllers, causing Laravel to mistakenly double prefix it for you. This attribute was removed and now you can import and use it without the issue.<\/p>\n\n\n\n<p>It can also be used for&nbsp;<a href=\"https:\/\/laravel.com\/docs\/7.x\/controllers#single-action-controllers\">single action controllers that<\/a>&nbsp;have the&nbsp;<code>__invoke<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Route::get(&#8216;\/welcome&#8217;, [WelcomeController::class, &#8216;index&#8217;]);<\/p>\n\n\n\n<p> Route::get(&#8216;\/welcome&#8217;, WelcomeController::class); <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>4. Route Caching<\/strong><\/p>\n\n\n\n<p>Laravel uses route caching to compile your routes in a PHP array that is more efficient to deal with. In Laravel 8, it\u2019s possible to use this feature even if you have closures as actions to your routes. This should extend the usage of route caching for improved performance.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Route::get(&#8216;\/components&#8217;, function(){<\/p>\n\n\n\n<p>     return view(&#8216;button&#8217;);<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<p><strong>5. Attributes on Extended Blade Components<\/strong><\/p>\n\n\n\n<p>In Laravel 7 the child components didn\u2019t have access to the&nbsp;<code>$attributes<\/code>&nbsp;passed to it. In Laravel 8 these components were enhanced and it is now possible to merge nested component attributes. This makes it easier to create extended components.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>6. Better Syntax for Event Listening<\/strong><\/p>\n\n\n\n<p>In the previous versions of Laravel, when creating a closure-based event listener there was much repetition and a cumbersome syntax.<\/p>\n\n\n\n<p>Event::listen(ConferenceScheduled::class, function( ConferenceScheduled  $event){<\/p>\n\n\n\n<p>   info(get_class($event));<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<p>In Laravel 8 it\u2019s simpler and cleaner:<\/p>\n\n\n\n<p>Event::listen(function( ConferenceScheduled  $event){<\/p>\n\n\n\n<p>   info(get_class($event));<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<p>\/\/one line version<\/p>\n\n\n\n<p>Event::listen(fn( ConferenceScheduled  $event =&gt;info(get_class($event))));<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>7. Queueable Anonymous Event Listeners<\/strong><\/p>\n\n\n\n<p>In Laravel 8 it is possible to create queueable closure from anywhere in the code. This will create a&nbsp;<a href=\"https:\/\/laravel.com\/docs\/8.x\/queues\">queue<\/a>&nbsp;of anonymous event listeners that will get executed in the background. This feature makes it easier to do this, while in previous versions of Laravel you would need to use an event class and an event listener (using the ShouldQueue trait).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>&lt;?php<\/p>\n\n\n\n<p>namespace App\\Models;<\/p>\n\n\n\n<p>use function Illuminate\\Events\\queueable;<\/p>\n\n\n\n<p>Class User extends Authenticable<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>  protected static function booting()<\/p>\n\n\n\n<p> {<\/p>\n\n\n\n<p>   static::created( queueable ( function (User $user) {<\/p>\n\n\n\n<p>     info(&#8216; Queued: &#8216;.$user-&gt;name);<\/p>\n\n\n\n<p>  });<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><strong>8. Maintenance Mode<\/strong><\/p>\n\n\n\n<p><strong>artisan down<\/strong> &#8212;<strong>secret=laracon-2020<\/strong><\/p>\n\n\n\n<p>This is especially useful when you need to do some maintenance on your application and you want to take it down for your users but still let your developers investigate bugs. This will create a secret cookie that will be granted to whoever hits the correct endpoint, allowing it to use the application while in maintenance mode.<\/p>\n\n\n\n<p><strong>artisan down &#8211;render=&#8221;errors:503&#8243;<\/strong><\/p>\n\n\n\n<p>Pre-rendering an error view is a safe way for not exposing errors to your end user when your application is down (during a new deployment, for example). The Laravel 8 framework guarantees that your predefined error page will be displayed before everything else from the application.<\/p>\n\n\n\n<p> <strong>artisan down &#8211;render=&#8221;welcome&#8221;<\/strong>  <strong>&#8211;redirect=\/ &#8211;status=200 &#8211;secret=laracon-2020<\/strong><\/p>\n\n\n\n<p>This combines the new features and will make the application only display a single predefined route, while still allowing for the people who have the secret to test and debug. This can be very useful when launching a new app.<\/p>\n\n\n\n<p><strong>9. Closure Dispatch \u201cCatch\u201d<\/strong><\/p>\n\n\n\n<p>Route::get(&#8216;\/queue-catch&#8217;, function(){<\/p>\n\n\n\n<p>    dispatch( function() {<\/p>\n\n\n\n<p>         throw new Exception(&#8216;Something went wrong&#8230;&#8217;);<\/p>\n\n\n\n<p>    })-&gt;catch(function( Throwable $e){<\/p>\n\n\n\n<p>         info(&#8216;Caught exception!!&#8217;);<\/p>\n\n\n\n<p>    })<\/p>\n\n\n\n<p>});<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Laravel has a pretty robust queue system that accepts a closure queue that will get serialized and executed in the background. Now we have a way to handle failures in case your job fails.<\/p>\n\n\n\n<p><strong>10. Exponential Backoff Strategy<\/strong><\/p>\n\n\n\n<p>This is an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Exponential_backoff\">algorithm<\/a>&nbsp;that decreases the rate of your job in order to gradually find an acceptable rate.<\/p>\n\n\n\n<p>public function backoff(){<\/p>\n\n\n\n<p>    return [1,5,10];<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Tsuki<\/p>\n\n\n\n<p><\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11094\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/11094\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"The 10 best new features in Laravel 8\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11094\" data-text=\"The 10 best new features in Laravel 8\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11094\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/11094\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>1. app\/Models Directory The&nbsp;artisan:make model&nbsp;command will create the model in the app\/Models direc [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11094"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=11094"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11094\/revisions"}],"predecessor-version":[{"id":11103,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/11094\/revisions\/11103"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=11094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=11094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=11094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}