Laravel 5.7.10 Released now - Check new laravel 5.7 Features

by jay patel




The Laravel team released Laravel 5.7.10  on 23 October 2018 with a handful of new features, fixes, and changes:

loadCount Eloquent Collection Method

First up, is the ability to load relationship counts on an Eloquent collection. Before this feature, you could only load relationships, but now you can call loadCount() to get counts for all relations.

The pull request illustrates how you could use loadCount() with the following example:

 

 

$events   =   Event :: latest( ) -> with( 'eventable' ) -> paginate ( );

$groups  =   $events ->map ( function ($event)   {

return  $event ->eventable;

} ) ->groupBy( function  ($eventable)   {

return  get_class ($eventable) ;

} ) ;

$groups [Post::class] -> loadCount ( 'comments' ) ;

$groups [Comment::class] -> loadCount ( 'hearts' ) ;

return  new EventIndexResponse ( $events ) ;

 

 

Now all the eventable  models would have a count value for comments or hearts respectively. The loadCount feature was added by Tim MacDonald. Nice work Tim!

 

Pass a Model to assertSoftDeleted

You can use the assertSoftDeleted to assert that a model has been soft deleted. Before Laravel 5.7.10, the method call might look something like the following:

 

$this ->assertSoftDeleted( 'users' ,  [

'id'  =>  $user ->id,

'name'  =>  $user->name, 

'email'  =>  $deletedUser ->email,

] ) ;

 

 

Now, thanks to an update to the assertSoftDeleted method, you can pass the model directly which will assert against the table’s primary key and primary key value:

 

 

$this ->assertSoftDeleted ( $user ) ;

//  Same as:

$this ->assertSoftDeleted( 'users' ,  [ 'id'  =>  $user ->id ] ) ;

 

 

Thanks to Dwight Watson for this nice shortcut!

 

Add Mock and Spy Objects to the Container

Titas Gailius added the ability to more conveniently add mock and spy instances to the container in a test:

 

 

$this -> mock ( Mailer :: class,   function  ( $mock )    {

$mock -> shouldReceive ( ' send ' ) ->once ( ) ;

} ) ;

$this -> spy ( Dispatcher::class,   function  ( $mock )    {

$mock -> shouldReceive ( ' dispatch ' ) -> andReturn ( false ) ;

} ) ;

 

 

These methods are syntactic sugar for something like the following:

 

 

$this -> instance ( Mailer :: class,   Mockery :: mock ( Mailer :: class,   function  ( $mock )   {

$mock  ->  shouldReceive  ( ' send ' ) ->once ( ) ;

} ) ) ;

 

 

UUID Validation

André Filipe contributed a UUID validation method which uses a regex to avoid dependency of ramsey/uuid  in the illuminate/validation component.

The name of the validation rule is uuid:

 

 

$request -> validate ( [

' post ' => ' required|uuid '

] ) ;

 

 

Notification Fake can Assert a Preferred Locale

Derek MacDonald added the ability to assert the preferred notification fake locale on notifications that implement the HasLocalePreference interface:

 

 

Notification :: fake ( ) ;

$user   =   factory ( User :: class ) -> create ( [ 'locale' => 'au'] );

//  Call  an  artisan  console  command  that  invokes:

Notification :: send ( $user,   new  AFLTrade ( ' Chad Wingard ' ,  ' Hawthorn ' ) );

Notification  ::  assertSentTo ($user,  AFLTrade :: class ,  function  ( $notification,   $channels,   $notifiable,   $locale) use  ( $user )  {

return  $notification -> player   ===   ' Chad Wingard '  &&

$notification -> club   ===   ' Hawthorn ' &&

$notifiable   ===   $user   &&

$locale   ===   ' au ' ;

} ) ;

 

You can see the full diff between 5.7.9 and 5.7.10 on GitHub



Leave a Reply

Your email address will not be published. Required fields are marked *

   Confirm you are not a spammer
   Notify me of follow-up comments by email.