Blade Component Slot Attributes in Laravel 8.56

News

August 26th, 2021

laravel8.jpg

The Laravel team released 8.56 with a collections firstOrFail() method, Blade component slot attributes, default conditional validation rules, and the latest changes in the 8.x branch:

Validation Assertion Improvements

Dwight Watson contributed validation assertion improvements to assertInvalid that brings quality-of-life improvements:

1// Generically assert there are errors
2$response->assertInvalid();
3 
4// Array was required previously
5// Now you can pass a string key
6$response->assertInvalid(['name', 'email']);
7 
8$response->assertInvalid('email');

Blade Component Slot Attributes

Dan Harrin contributed the ability for blade component slots to have their own attributes:

1<x-card class="shadow-sm">
2 <x-slot name="heading" class="font-bold">
3 Heading
4 </x-slot>
5 
6 Content
7 
8 <x-slot name="footer" class="text-sm">
9 Footer
10 </x-slot>
11</x-card>

To learn more, check out the code and discussion in Pull Request #38372.

Collections "firstOrFail()" Method

@powellblyth contributed a firstOrFail() method to collections (and lazy collections) which makes things a little more fluent:

1// Without `firstOrFail()`
2$collection = new Collection([
3 ['name' => 'foo'],
4 ['name' => 'foo'],
5 ['name' => 'bar'],
6]);
7 
8// Slightly obscure code and hard to scan
9if ($collection->where('name', 'fish')->count() === 0){
10 $collection->add('fish');
11}
12 
13$this->doSomethingWith($collection);

Using firstOrFail() a missing value will throw an exception:

1$collection = new Collection([
2 ['name' => 'foo'],
3 ['name' => 'foo'],
4 ['name' => 'bar'],
5]);
6 
7try {
8 $collection->where('name', 'fish')->firstOrFail()
9}
10// An Exception? This must be important, I'm glad I scanned it
11catch (ItemNotFoundException){
12 // Ensure user has 'fish' in their collection
13 $collection->add('fish');
14}
15 
16$this->doSomethingWith($collection);

Default Rules for Conditional Validation

@bastien-phi contributed default rules when a condition is false instead of having to write the inverse conditional rule:

1return [
2 'email' => [
3 Rule::when($this->someComplexTest(), ['required']),
4 Rule::when(!$this->someComplexTest(), ['nullable']),
5 ],
6];

Now you can pass the default rule as the third argument:

1return [
2 'email' => [
3 Rule::when(
4 $this->someComplexTest(),
5 ['required'],
6 // When the condition is `false`...
7 ['nullable']
8 ),
9 ],
10];

Get Full Request URL Without Query Params

Ali Saleem contributed a fullUrlWithoutQuery() request method that allows you to remove specific parameters from the query string:

1// Example: https://example.com/?color=red&shape=square&size=small
2 
3// Removes `color`
4request()->fullUrlWithoutQuery('color');
5// https://example.com/?shape=square&size=small
6 
7// Removes `color` and `size`
8request()->fullUrlWithoutQuery(['color', 'size']);
9// https://example.com/?shape=square

Release Notes

You can see the full list of new features and updates below and the diff between 8.55.0 and 8.56.0 on GitHub. The following release notes are directly from the changelog:

v8.56.0

Added

  • Added firstOrFail to Illuminate\Support\Collections and Illuminate\Support\LazyCollections (#38420)
  • Support route caching with trashed bindings (c3ec2f2)
  • Allow only keys directly on safe in FormRequest (5e4ded8)
  • Added default rules in conditional rules (#38450)
  • Added fullUrlWithoutQuery method to Request (#38482)
  • Added --implicit (and -i) option to make:rule (#38480)
  • Added colon port support in serve command host option (#38522)

Changed

  • Testing: Access component properties from the return value of $this->component() (#38396, 42a71fd)
  • Update InteractsWithInput::bearerToken() (#38426)
  • Minor improvements to validation assertions API (#38422)
  • Blade component slot attributes (#38372)
  • Convenient methods for rate limiting (2f93c49)
  • Run event:clear on optimize:clear (a61b24c2)
  • Remove unnecessary double MAC for AEAD ciphers (#38475)
  • Adds Response authorization to Form Requests (#38489)
  • Make TestResponse::getCookie public so it can be directly used in tests (#38524)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.