Laravel 5.5 LTS is Now Released

  Version 5.5 of Laravel is now officially released! This release is jam-packed with goodies and improvements–here’s a quick video summarizing the highlight features:  

Laravel 5.5 is the Next LTS Release

Laravel 5.5 is the next long term support (LTS) version of Laravel (the last being 5.1). LTS versions receive bug fixes for two years, and security fixes for three years. General minor releases receive bug fixes for six months and security fixes for one year.

Whoops Package

You might recall the filp/whoops package from Laravel v4 provided elegant stack traces for your most frustrating debugging moments. The whoops package returns to Laravel 5.5!

Collection Dumping

Another great debugging feature (that I’ve seen previously as a package or macro) is collection dumping methods:

<?php  Song::all()     ->filter     ->platinum     ->dump()     ->filter(function ($song) {         return $song->released_on >= CarbonCarbon::parse('-10 years');     })     ->dd(); 

Read our collection dumping post for more details.

Exception Rendering

Exceptions now can render a response if they define a public “response” method. Typically, in earlier versions of Laravel you might add a check to the AppExceptionsHandler::render() method, and conditionally send back a response based on the exception type. In 5.5, you can just throw the exception, and it can respond without additional logic in your handler:

<?php  // throw new TerribleSongException($song) in a controller...  namespace AppExceptions;  use AppSong;  class TerribleSongException extends Exception {     /**      * @var AppSong      */     protected $song;      public function __construct(Song $song)     {         $this->song = $song;     }      /**      * @param IlluminateHttpRequest $request      */     public function render($request)     {         return response("The song '{$this->song->title}' by '{$this->song->artist}' is terrible.");         } } 

You can also implement the Responsable interface in your exception classes, and Laravel will respond automatically.

The Responsable Interface

The Responsable interface is another response addition to laravel that we’ve covered at Laravel news. A class implementing the interface can be returned from a controller method; the router now checks for an instance of Responsable when preparing the response from IlluminateRoutingRouter. Here’s what an example might look like, leaving the response details to the NewSongResponse object:

public function store(Request $request) {     $data = request()->validate([         'title' => 'required',         'artist' => 'required',         'description' => 'required',         'duration' => 'required|numeric',         'released_on' => 'required|date_format:Y-m-d',         'gold' => 'boolean',         'platinum' => 'boolean',     ]);      $song = new Song($data);     $song->save();      return new NewSongResponse($song); } 

Here’s what the class might look like implementing the Responsable interface for a new song creation:

<?php  namespace AppHttpResponses;  use AppSong; use IlluminateContractsSupportResponsable;  class NewSongResponse implements Responsable {     /**      * @var AppSong      */     protected $song;      /**      * @param AppSong $song      */     public function __construct(Song $song)     {        $this->song = $song;      }      public function toResponse($request)     {         if ($request->wantsJson()) {             return response()                 ->json($this->song)                 ->header('Location', route('songs.show', $this->song))                 ->setStatusCode(201);         }          return redirect()             ->route('songs.show', $this->song);     } } 

In this simple example, you could automatically respond with JSON if you make a request via AJAX, and by default response with a redirect the songs.show route.

Request Validation Method

In past versions of Laravel you would pass the request instance to the $this->validate() method in a controller:

$this->validate(request(), [...]); 

Now, you can just call validate on the request object:

$data = request()->validate([     'title' => 'required',     'artist' => 'required',     'description' => 'required',     'duration' => 'required|numeric',     'released_on' => 'required|date_format:Y-m-d',     'gold' => 'boolean',     'platinum' => 'boolean', ]); 

Another nice benefit from this style of calling validation is that the return value acts like Request::only(), returning only the keys provided in the validation call. Returning only the validated keys is an excellent convention to use, avoiding Request::all().

Custom Validation Rule Objects and Closures

My favorite feature in Laravel 5.5 is hands-down the new custom validation rule objects and closures. Creating a custom rule object is an excellent alternative to creating custom rules with Validator::extend (which you can still use), because it’s more clear where the rule logic is located at a glance. A validation rule object might look like this:

<?php  namespace AppRules;  use IlluminateContractsValidationRule;  class CowbellValidationRule implements Rule {     public function passes($attribute, $value)     {         return $value > 10;     }      public function message()     {         return ':attribute needs more cowbell!';     } } 

An example of using this validation rule looks like the following:

<?php  request()->validate([     'cowbells' => [new CowbellValidationRule],     'more_cowbells' => [function ($attribute, $value, $fail) {         if ($value <= 10) {             $fail(':attribute needs more cowbell!');         }     }] ]); 

The closure style takes the attribute and value, and a fail parameter that you call if the validation rule should fail. The closure is a nice way to experiment with custom validation before you extract it to a dedicated rule object, or for one-off custom validation needs. To create custom validation rule objects, you can use the new make:rule command:

$ php artisan make:rule MyCustomRule 

We have a dedicated post to custom validation rules here on Laravel News, be sure to check it out!

Auth and Guest Blade Directives

We have written about Blade::if() directives in 5.5. A few new conditional directives in 5.5 are @auth and @guest. Typically you might use something like the following to check for an authenticated user in Blade:

@if(auth()->check())     {{ -- authenticated --}} @endif  @if(auth()->guest()) 

You can now use the following directives to achieve the same thing:

@auth     Welcome {{ user()->name }}! @endauth  @guest     Welcome Guest! @endguest 

Frontend Presets

When you are starting a new project, Laravel 5.5 provides Vue.js scaffolding by default. In Laravel 5.5 you can now pick from a few presets and remove all frontend scaffolding with the “preset” Artisan command in Laravel 5.5. If you look at the help, you can see that it allows you to pick “none,” “bootstrap,” “vue”, or “react”:

php artisan help preset Usage:   preset <type>  Arguments:   type    The preset type (none, bootstrap, vue, react)  # Use react $ php artisan preset react  # Clear scaffolding $ php artisan preset none 

Separate Factory Files

Factory files were previously defined in one ModelFactory.php file. Now, you create different files for each model. You can create a factory file when you are creating a new model:

$ php artisan make:model -fm Post  # Or you can create a controller, migration, and factory $ php artisan make:model --all 

You can also create a factory file directly with “make:factory”:

$ php artisan make:factory --model=Example ExampleFactory 

The migrate:fresh Migration Command

The new “migrate:fresh” migration command 5.5 is a nice addition to creating a clean database in development. The migrate:fresh command drops all the database tables and then runs the migrations. You might be familiar with the existing migrate:refresh command, which rolls back migrations and then reruns them. Usually in development you want to just drop the tables, getting a fresh database, and running migrations.

The RefreshDatabase Trait

On the testing front, the RefreshDatabase trait is the new way to migrate databases during tests. This new trait takes the most optimal approach to migrating your test database depending on if you are using an in-memory database or a traditional database. The DatabaseTransactions and DatabaseMigrations traits are still available in 5.5, allowing you to upgrade without using the new RefreshDatabase trait.

The withoutExceptionHandling() method

The base test case inherits a method withoutExceptionHandling(), which allows you to disable exception handling for a test. Disabling exception handling allows you to catch the exception in your test and assert the exception instead of the exception handler responding. It’s also a useful debugging tool when your test is doing something you don’t expect, and you want to see the actual exception.

Automatic Package Discovery

The last feature we are going to look at is automatic package discovery. While Laravel packages aren’t usually hard to install, the package detection feature means you don’t have to set up providers or aliases. You can disable auto-discovery for specific packages. Source via Laravel

Hi, I’m self-motivated professional, possess 6 years of comprehensive expertise in software design and development.

As the Managing Director & CTO of Elegant Tech, I’m always making sure that the development team is following the best practices and the latest standards in the industry.

On the other hand, I’m helping Elegant Tech’s clients to fulfill their needs through developing to them an end to end solutions that would help and ease their business tasks.

Starting new project? Book your free consultation session with me.

You Might Also Like

CONTACT US
221, Mount Olimpus, Rheasilvia, Mars,
Solar System, Milky Way Galaxy
+1 (999) 999-99-99
PGlmcmFtZSBzcmM9Imh0dHBzOi8vd3d3Lmdvb2dsZS5jb20vbWFwcy9lbWJlZD9wYj0hMW0xOCExbTEyITFtMyExZDYwNDQuMjc1NjM3NDU2ODA1ITJkLTczLjk4MzQ2MzY4MzI1MjA0ITNkNDAuNzU4OTkzNDExNDc4NTMhMm0zITFmMCEyZjAhM2YwITNtMiExaTEwMjQhMmk3NjghNGYxMy4xITNtMyExbTIhMXMweDAlM0EweDU1MTk0ZWM1YTFhZTA3MmUhMnNUaW1lcytTcXVhcmUhNWUwITNtMiExc2VuITJzITR2MTM5MjkwMTMxODQ2MSIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZnJhbWVib3JkZXI9IjAiIHN0eWxlPSJib3JkZXI6MCI+PC9pZnJhbWU+
Thank You. We will contact you as soon as possible.
COMPANY NAME
Dolor aliquet augue augue sit magnis, magna aenean aenean et! Et tempor, facilisis cursus turpis tempor odio. Diam lorem auctor sit, a a? Lundium placerat mus massa nunc habitasse.
  • Goblinus globalus fantumo tubus dia montes
  • Scelerisque cursus dignissim lopatico vutario
  • Montes vutario lacus quis preambul denlac
  • Leftomato denitro oculus softam lorum quis
  • Spiratio dodenus christmas gulleria tix digit
  • Dualo fitemus lacus quis preambul patturtul
CONTACT US
Thank You. We will contact you as soon as possible.
PGlmcmFtZSBzcmM9Imh0dHBzOi8vd3d3Lmdvb2dsZS5jb20vbWFwcy9lbWJlZD9wYj0hMW0xOCExbTEyITFtMyExZDYwNDQuMjc1NjM3NDU2ODA1ITJkLTczLjk4MzQ2MzY4MzI1MjA0ITNkNDAuNzU4OTkzNDExNDc4NTMhMm0zITFmMCEyZjAhM2YwITNtMiExaTEwMjQhMmk3NjghNGYxMy4xITNtMyExbTIhMXMweDAlM0EweDU1MTk0ZWM1YTFhZTA3MmUhMnNUaW1lcytTcXVhcmUhNWUwITNtMiExc2VuITJzITR2MTM5MjkwMTMxODQ2MSIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZnJhbWVib3JkZXI9IjAiIHN0eWxlPSJib3JkZXI6MCI+PC9pZnJhbWU+
Thank You. We will contact you as soon as possible.
COMPANY NAME
CONTACT US
Dolor aliquet augue augue sit magnis, magna aenean aenean et! Et tempor, facilisis cursus turpis tempor odio putonius mudako empero brutto populius giten facilisis cursus turpis balocus tredium todo.
Thank You. We will contact you as soon as possible.
PGlmcmFtZSBzcmM9Imh0dHBzOi8vd3d3Lmdvb2dsZS5jb20vbWFwcy9lbWJlZD9wYj0hMW0xOCExbTEyITFtMyExZDYwNDQuMjc1NjM3NDU2ODA1ITJkLTczLjk4MzQ2MzY4MzI1MjA0ITNkNDAuNzU4OTkzNDExNDc4NTMhMm0zITFmMCEyZjAhM2YwITNtMiExaTEwMjQhMmk3NjghNGYxMy4xITNtMyExbTIhMXMweDAlM0EweDU1MTk0ZWM1YTFhZTA3MmUhMnNUaW1lcytTcXVhcmUhNWUwITNtMiExc2VuITJzITR2MTM5MjkwMTMxODQ2MSIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZnJhbWVib3JkZXI9IjAiIHN0eWxlPSJib3JkZXI6MCI+PC9pZnJhbWU+
CONTACT US
COMPANY NAME
221, Mount Olimpus, Rheasilvia, Mars
Solar System, Milky Way Galaxy
+1 (999) 999-99-99
Thank You. We will contact you as soon as possible.
Macbook Pro
* Intel Core i7 (3.8GHz, 6MB cache)
* Retina Display (2880 x 1880 px)
* NVIDIA GeForce GT 750M (Iris)
* 802.11ac Wi-Fi and Bluetooth 4.0
* Thunderbolt 2 (up to 20Gb/s)
* Faster All-Flash Storage (X1)
* Long Lasting Battery (9 hours)
PGlmcmFtZSB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBzcmM9Ii8vd3d3LnlvdXR1YmUuY29tL2VtYmVkL3NCV1BDdmR2OEJrP2F1dG9wbGF5PTEiIGZyYW1lYm9yZGVyPSIwIiBhbGxvd2Z1bGxzY3JlZW4+PC9pZnJhbWU+
ENJOY AURORA BOREALIS
SHARE
OR
SUBSCRIBE
Vel eros amet amet mauris a habitasse scel erisque? Vel urna dis et, placerat phasellus, diam in! Placerat nec facilisis, tortor tristique. Arcu placerat sagittis, velit lorem scelerisque egestas placerat phasellus.
PLEASE SHARE US!
Vel eros amet amet mauris a habitasse scelerisque? Vel urna dis et, placerat phasellus, diam in! Placerat nec facilisis, tortor tristique. Arcu placerat sagittis, velit lorem scelerisque egestas. Lundium et, ultrices, et tempor vel proin est! Lundium sociis ac, ut ultricies ridiculus ultricies pulvinar scelerisque et adipiscing auctor, urna platea non rhoncus magna egestas montes platea sed porta nisi porta, mus integer porta elit.
PGlmcmFtZSB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBzcmM9Imh0dHA6Ly93d3cueW91dHViZS5jb20vZW1iZWQvajhsU2NITzJtTTAiIGZyYW1lYm9yZGVyPSIwIiBhbGxvd2Z1bGxzY3JlZW4+PC9pZnJhbWU+
Increase more than 700% of Email Subscribers!
Dolor aliquet augue augue sit magnis, magna aenean aenean et! Et tempor, facilisis cursus turpis tempor odio. Diam lorem auctor sit, a a? Lundium placerat mus massa nunc habitasse, arcu, etiam pulvinar.
  • Goblinus globalus fantumo tubus dia
  • Scelerisque cursus dignissim lopatico
  • Montes vutario lacus quis preambul den
  • Leftomato denitro oculus softam lorum
  • Spiratio dodenus christmas gulleria tix
  • Dualo fitemus lacus quis preambul pat
  • Montes vutario lacus quis digit turtulis
  We hate spam and never share your details.
ARE YOU READY? GET IT NOW!
Increase more than 500% of Email Subscribers!
Your Information will never be shared with any third party.
SUBSCRIBE TO NEWSLETTER
Turpis dis amet adipiscing hac montes odio ac velit? Porta, non rhoncus vut, vel, et adipiscing magna pulvinar adipiscing est adipiscing urna. Dignissim rhoncus scelerisque pulvinar?
GET IT NOW
500% MORE SUBSCRIBERS
EFFICIENCY
BOOST YOUR SALES
CREATIVITY
INSPIRE YOUR VISITORS
BE UNIQUE
CREATE UNIQUE POPUPS
ARE YOU READY? GET IT NOW!
Increase more than 500% of Email Subscribers!
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis viverra, urna vitae vehicula congue, purus nibh vestibulum lacus, sit amet tristique ante odio.
ENTER YOUR NAME
ENTER YOUR E-MAIL
ENTER YOUR PHONE NUMBER
Your Information will never be shared with any third party.
SUBSCRIBE TO NEWSLETTER
All rights reserved © Company Name, 2014
Dolor aliquet augue augue sit magnis, magna aenean aenean et! Et tempor, facilisis cursus turpis tempor odio, cursus montes ac turpis. Ultrices! Massa integer augue ridiculus adipiscing, massa cras pid. Turpis placerat scelerisque, vut odio mus non, mattis porttitor, nunc odio, turpis tortor sit? Pid amet, sed facilisis.
  • Goblinus globalus fantumo tandempo
  • Scelerisque cursus dignissim donus
  • Montes vutario lacus quis arcupolisio
  • Leftomato denitro oculus tepircos den
  • Spiratio dodenus christmas popupius
  • Afrenius globalus spiritum tandempo
  • Fitatos vutario lacus quis arcup delis
Do you want massive traffic?
Dignissim enim porta aliquam nisi pellentesque. Pulvinar rhoncus magnis turpis sit odio pid pulvinar mattis integer aliquam!
  • Goblinus globalus fantumo tubus dia montes
  • Scelerisque cursus dignissim lopatico vutario
  • Montes vutario lacus quis preambul den lacus
  • Leftomato denitro oculus softam lorum quis
  • Spiratio dodenus christmas gulleria tix digit
  • Dualo fitemus lacus quis preambul pat turtulis
* we never share your e-mail with third parties.
Do you want more traffic?
Dignissim enim porta aliquam nisi pellentesque. Pulvinar rhoncus magnis turpis sit odio pid pulvinar mattis integer aliquam!
  • Goblinus globalus fantumo tubus dia montes
  • Scelerisque cursus dignissim lopatico vutario
  • Montes vutario lacus quis preambul den lacus
  • Leftomato denitro oculus softam lorum quis
  • Spiratio dodenus christmas gulleria tix digit
  • Dualo fitemus lacus quis preambul pat turtulis
  • Scelerisque cursus dignissim lopatico vutario
  • Montes vutario lacus quis preambul den lacus
SUBSCRIBE TO OUR NEWSLETTER AND START INCREASING YOUR PROFITS NOW!
* we never share your e-mail with third parties.
Do you want massive traffic?
Scelerisque augue ac hac, aliquet, nascetur turpis. Augue diam phasellus odio lorem integer, aliquam aliquam sociis nisi adipiscing hacac.
  • Goblinus globalus fantumo tubus dia
  • Scelerisque cursus dignissim lopatico
  • Montes vutario lacus quis preambul
  • Leftomato denitro oculus softam lorum
  • Spiratio dodenus christmas gulleria tix
  • Dualo fitemus lacus quis preambul bela
PGlmcmFtZSB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBzcmM9Imh0dHA6Ly93d3cueW91dHViZS5jb20vZW1iZWQvajhsU2NITzJtTTAiIGZyYW1lYm9yZGVyPSIwIiBhbGxvd2Z1bGxzY3JlZW4+PC9pZnJhbWU+
* we never share your details with third parties.
Impressed you enough?
Subscribe today to receive the latest articles and news in the software development industry.
Your Information will never be shared with any third party.