Laravel 9.26 Released

Laravel 9.26 Released

Laravel 9.26 released with Models non-backed enums supports, vite asset url helper, SqlServerConnector Authentication Keyword, route grouping with where* methods, min and max digits validation, closure support to dispatch condition and more including the fixes. Let’s check these updates in detail here:

1. non-backed enums in Models:

Ollie Read contributed to Adding support for non-backed enums in Models. It adds support for non-backed enums when casting eloquent attributes. It works based on the fact that enum cases are glorified constants, so the constant() the method will work for retrieving.

Source File Edits: src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

  • getEnumCastableAttributeValue:
/**
 * Cast the given attribute to an enum.
 *
 * @param  string  $key
 * @param  mixed  $value
 * @return mixed
 */
protected function getEnumCastableAttributeValue($key, $value)
{
    if (is_null($value)) {
        return;
    }

    $castType = $this->getCasts()[$key];

    if ($value instanceof $castType) {
        return $value;
    }

    if (is_subclass_of($castType, \BackedEnum::class)) {
        return $castType::from($value);
    }

    return constant($castType.'::'.$value);
}
  • setEnumCastableAttribute:
/**
 * Set the value of an enum castable attribute.
 *
 * @param  string  $key
 * @param  \UnitEnum|string|int  $value
 * @return void
 */
protected function setEnumCastableAttribute($key, $value)
{
    $enumClass = $this->getCasts()[$key];

    if (! isset($value)) {
        $this->attributes[$key] = null;
    } elseif (is_subclass_of($enumClass, \BackedEnum::class)) {
        if ($value instanceof $enumClass) {
            $this->attributes[$key] = $value->value;
        } else {
            $this->attributes[$key] = $enumClass::from($value)->value;
        }
    } else {
        if ($value instanceof $enumClass) {
            $this->attributes[$key] = $value->name;
        } else {
            $this->attributes[$key] = constant($enumClass.'::'.$value)->name;
        }
    }
}

It also updates the $value param type for Illuminate\Database\Eloquent\Concerns\HasAttributes::setEnumCastableAttribute from \BackedEnum to \UnitEnum|string|int. It’s worth noting that the original version should have technically been \BackedEnum|string|int because there are code paths for handling when it isn’t an instance.

2. vite asset url helpers:

Tim MacDonald contributed  Vite::asset() helper to create the asset URLs in blade file. When using plain Blade as templating platform, it can be useful to still process images that are not referenced in your JS with Vite – it may even be the case that you don’t have any JS and still want to minify your projects’ images.

{
  "resources/images/logo.jpeg": {
    "file": "assets/logo.1ddf943b.jpeg",
    "src": "resources/images/logo.jpeg"
  },
  "resources/js/app.js": {
    "file": "assets/app.e5198d1b.js",
    "src": "resources/js/app.js",
    "isEntry": true
  }
}

profile image URL will be generated in Blade with the following:

<img src="{{ Vite::asset('resources/images/logo.jpeg') }}">

Which would result in the following HTML:

<img src="https://asset-url.com/build/assets/logo.1ddf943b.jpeg">

In build mode, this will generate a URL to the dev server, resulting in the following HTML:

<img src="https://[::1]:5173/resources/images/logo.jpeg">

3. SQL Server Connection Authentication keyword:

Matt Davis contributed to adding the Authentication keyword while connecting with SQLServcer. Existing functionality does not allow the Authentication keyword to be passed into the DSN when connecting to a SQL Server instance. This prevents developers connecting to a SQL Server instance using Azure AD.

Setting the authentication keyword is done by adding the authentication option to the DSN string. Example:

"sqlsrv:Server=server;Database=dbname;Authentication=ActiveDirectoryPassword"

Source File Edits: src/Illuminate/Database/Connectors/SqlServerConnector.php

/**
 * Get the DSN string for a SqlSrv connection.
 *
 * @param  array  $config
 * @return string
 */
protected function getSqlSrvDsn(array $config)
{
	$arguments = [
        'Server' => $this->buildHostString($config, ','),
    ];

    ...

	if (isset($config['authentication'])) {
        $arguments['Authentication'] = $config['authentication'];
    }

    return $this->buildConnectString('sqlsrv', $arguments);
}

References:
Microsoft Guide – Connect via Azure AD Auth
Microsoft connection options documentation

4. “Where” methods to route groups:

Ollie Read contributed to adding support for additional where* methods available on a route group, by adding the CreatesRegularExpressionRouteConstraints trait. It works directly on the router, handled by Router::__call with an explicit check for beginning with where, that proxies to the method.

Route::whereIn(['foo', 'bar'], ['one', 'two'])
  ->prefix('/{foo}/{bar}')
  ->group(function () {
    	// ...
  });

And on a RouteRegistrar, using the trait.

Route::prefix('/{foo}/{bar}')
  ->whereIn(['foo', 'bar'], ['one', 'two'])
  ->group(function () {
    // ...
  });

Source File Edits: src/Illuminate/Routing/Router.php

/**
 * Dynamically handle calls into the router instance.
 *
 * @param  string  $method
 * @param  array  $parameters
 * @return mixed
 */
public function __call($method, $parameters)
{
    ...

    if ($method !== 'where' && Str::startsWith($method, 'where')) {
        return (new RouteRegistrar($this))->{$method}(...$parameters);
    }

    return (new RouteRegistrar($this))->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true);
}

5. Min and Max digits Validation Rule:

Dan Harrin contributed to the min_digits and max_digits validation rule. As per the size rule, the number of digits in numeric input cannot be validated traditionally using min and max. Currently, we have, digits to validate the exact length of a number, and digits_between to validate the length within two boundaries. However, there is not yet a way to validate that a number has a minimum OR a maximum number of digits, just both. Dan Harrin introduces two validation rules – min_digits and max_digits for this use:

Validator::validate([
    'number' => 1000,
], [
    'number' => [
        'min_digits:3', 'max_digits:5', // Passes as `1000` has 4 digits
        'min:3', 'max:5', // Fails as `1000` is greater than 5
    ],
])

Source FIle Edits: src/Illuminate/Validation/Concerns/ValidatesAttributes.php

  • validateMaxDigits Method
/**
 * Validate that an attribute has a maximum number of digits.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @param  array<int, int|string>  $parameters
 * @return bool
 */
public function validateMaxDigits($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'max');

    $length = strlen((string) $value);

    return ! preg_match('/[^0-9]/', $value) && $length <= $parameters[0];
}
  • validateMinDigits Method
/**
 * Validate that an attribute has a minimum number of digits.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @param  array<int, int|string>  $parameters
 * @return bool
 */
public function validateMinDigits($attribute, $value, $parameters)
{
    $this->requireParameterCount(1, $parameters, 'min');

    $length = strlen((string) $value);

    return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0];
}

6. Closure support to dispatch conditionals:

Italo contributed to the usage of a Closure to dispatch jobs conditionally. Now, the closure will receive the job arguments or you can say entire job instance is passed to the closure with dispatchIf and dispatchUnless.

MyQueuableJob::dispatchIf(Something::check(...), $name);

// Dispatches job
MyQueuableJob::dispatchIf(
    fn ($job) => true,
    $name
);
 
// Will not dispatch
MyQueuableJob::dispatchUnless(
    fn ($job) => false,
    $name
);

Source File Edits: src/Illuminate/Foundation/Bus/Dispatchable.php

  • dispatchIf Method
/**
 * Dispatch the job with the given arguments if the given truth test passes.
 *
 * @param  bool|\Closure  $boolean
 * @param  mixed  ...$arguments
 * @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent
 */
public static function dispatchIf($boolean, ...$arguments)
{
    return value($boolean, ...$arguments)
        ? new PendingDispatch(new static(...$arguments))
        : new Fluent;
}
  • dispatchUnless Method
/**
 * Dispatch the job with the given arguments unless the given truth test passes.
 *
 * @param  bool|\Closure  $boolean
 * @param  mixed  ...$arguments
 * @return \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent
 */
public static function dispatchUnless($boolean, ...$arguments)
{
    return ! value($boolean, ...$arguments)
        ? new PendingDispatch(new static(...$arguments))
        : new Fluent;
}

7. Configurable paths to Vite:

D. Nagy Gergő contributed to adding configurable paths to Vite and the following new functions should be added to the Vite class:

  1. New func withEntryPoints that accepts entry points and is merged into an array on the class.
  2. New func useHotFile that accepts a path.
  3. New func useBuildDirectory that accepts a path.
  4. Make the Vite class Htmlable, with toHtml invoking the class.

Source File Edits: src/Illuminate/Foundation/Vite.php

  • withEntryPoints Method
/**
 * Set the Vite entry points.
 *
 * @param  array  $entryPoints
 * @return $this
 */
public function withEntryPoints($entryPoints)
{
    $this->entryPoints = $entryPoints;

    return $this;
}
  • useHotFile Method
/**
 * Set the Vite "hot" file path.
 *
 * @param  string  $path
 * @return $this
 */
public function useHotFile($path)
{
    $this->hotFile = $path;

    return $this;
}
  • useBuildDirectory Method
/**
 * Set the Vite build directory.
 *
 * @param  string  $path
 * @return $this
 */
public function useBuildDirectory($path)
{
    $this->buildDirectory = $path;

    return $this;
}
  • toHtml Method
/**
 * Get the Vite tag content as a string of HTML.
 *
 * @return string
 */
public function toHtml()
{
    return $this->__invoke($this->entryPoints)->toHtml();
}

Now the list of the Laravel 9.26 Released updates:

Added

  • Adding support for non-backed enums in Models (#43728)
  • Added vite asset url helpers (#43702)
  • Added Authentication keyword for SqlServerConnector.php (#43757)
  • Added support for additional where* methods to route groups (#43731)
  • Added min_digits and max_digits validation (#43797)
  • Added closure support to dispatch conditionals in bus (#43784)
  • Added configurable paths to Vite (#43620)

Fixed

  • Fix unique lock release for broadcast events (#43738)
  • Fix empty collection class serialization (#43758)
  • Fixes creation of deprecations channel (#43812)

Changed

  • Improve display of failures for assertDatabaseHas (#43736)
  • Always use the write PDO connection to read the just stored pending batch in bus (#43737)
  • Move unique lock release to method (#43740)
  • Remove timeoutAt fallback from Job base class (#43749)
  • Convert closures to arrow functions (#43778)
  • Use except also in Illuminate/Routing/Middleware/ValidateSignature::handle() (e554d47)
  • Adjust forever time for cookies (#43806)
  • Make string padding UTF-8 safe (f1762ed)

To get to know more about Laravel, you can check these articles too.

You can check the latest release tech articles like Laravel 9.26 Released here.

Please follow and like us:

Related Posts

Leave a Reply

Share