Posts

Since 2024
Monitor Code Processing Time in PHP with Time Warden
Monitor Code Processing Time in PHP with Time Warden

Time Warden is a lightweight PHP library that enables you to monitor the processing time of tasks and take actions on thresholds exceeding defined execution time limits. It's probably best illustrated with an example from the readme: timeWarden()->task('Checking articles')->start(); foreach ($articles as $article) { // Perform long process... πŸ•’ } // Using traditional anonymous function timeWarden()->stop(static function (Task $task): void { $task->onExceedsMilliseconds(500, static function (Task $task): void { // Do what you need, for example, send an email πŸ™‚ Mail::to('foo@bar.com')->queue( new SlowArticleProcess($task) ); }); }); // Or using an arrow function timeWarden()->stop(static function (Task $task): void { $task->onExceedsMilliseconds(500, fn (Task $task) => Log::error($task->name.' has taken too long')); }); This library has excellent documentation in the project's readme, which in summary offers the following features: Monitor processing time of critical tasks in development and debugging Reactive actions when time is exceeded (milliseconds, seconds, minutes, and hours) Execution time debugging output Measure execution time of an individual task and groups of tasks Framework-agnostic library you can use with Laravel, Symfony, standalone, etc. PHP 8.2+ I like the output of tasks, which you can output directly or through a logging system: echo timeWarden()->output(); /* ╔═════════════════════ TIMEWARDEN ═════╀═══════════════╗ β•‘ GROUP β”‚ TASK β”‚ DURATION (MS) β•‘ ╠═════════════════════β•ͺ════════════════β•ͺ═══════════════╣ β•‘ default (320.37 ms) β”‚ Articles task β”‚ 70.23 β•‘ β•‘ β”‚ Customers task β”‚ 250.14 β•‘ β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• Total: 320.37 ms ══╧═══════════════╝ */ // Send as a log if (app()->environment('local')) { Log::debug(timeWarden()->output()); } You can learn more about this package, get full installation instructions, and view the source code on GitHub. The post Monitor Code Processing Time in PHP with Time Warden appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

A New Minimal Default Exception Page With Dark Mode Support in Laravel 11.9
A New Minimal Default Exception Page With Dark Mode Support in Laravel 11.9

This week, the Laravel team released v11.9,Β which includesΒ a new default exception page, a way to prevent destructive commands from running, a withoutDelay() queue method, and more. New Default Exception Page Laravel now ships with a minimal default exception page when your application is in debug mode. The updated error page has light and dark mode support: New default exception page in Laravel 11.9 (dark mode) The Exception page will continue to render the default Symfony view (unless you've defined a custom renderer) when debug is false: Exception page when APP_DEBUG=false This update only affects new Laravel applications, so existing applications will still use Ignition if installed. If you would like to continue to use the Spatie Ignition exception page in new Laravel applications, you can install it with Composer: composer require spatie/laravel-ignition See Pull Request #51261 and #51587 for more details. Prevent Destructive Commands Jason McCreary and Joel Clermont contributed a Prohibitable trait along with code that prevents destructive commands from running. You can also add these to your custom Artisan commands that might have destructive behavior you don't intend to run in some environments (usually production): use Illuminate\Console\Command; use Illuminate\Console\Prohibitable; Β  class SomeDestructiveCommand extends Command { use Prohibitable; } Β  // SomeDestructiveCommand::prohibit($this->app->isProduction()); The Laravel framework includes some database commands that include the Prohibitable trait, such as db:wipe, migrate:fresh, migrate:refresh, and migrate:reset. You can prohibit them individually or use the DB Facade to prohibit all of the aforementioned commands: // Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset DB::prohibitDestructiveCommands($this->app->isProduction()); Add withoutDelay() to the Queueable trait Kennedy Tedesco contributed a withoutDelay() method to the Queueable trait. If a job has a default delay time, you can use this in cases where you want to skip that delay instead of passing 0 to the delay() method: dispatch((new MyJob($data))->delay(0)); dispatch((new MyJob($data))->withoutDelay()); Release notes You can seeΒ the complete list of new features and updates below and the diff between 11.8.0 and 11.9.0 on GitHub. The following release notes are directly from the changelog: v11.9.0 [11.x] Optimize boostrap time by using hashtable to store providers by @sarven in https://github.com/laravel/framework/pull/51343 [11.x] Prevent destructive commands from running by @jasonmccreary in https://github.com/laravel/framework/pull/51376 [11.x] renamed left has to contains by @MrPunyapal in https://github.com/laravel/framework/pull/51532 [10.x] Fix typo by @Issei0804-ie in https://github.com/laravel/framework/pull/51535 [11.x] Fixes doc block in Timebox.php by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51537 [11.x] Rename test function to match prohibit action by @faissaloux in https://github.com/laravel/framework/pull/51534 [11.x] Fix LazilyRefreshDatabase when using Laravel BrowserKit Testing by @MaxGiting in https://github.com/laravel/framework/pull/51538 [10.x] Fix SQL Server detection in database store by @staudenmeir in https://github.com/laravel/framework/pull/51547 [11.x] Display test creation messages by @nshiro in https://github.com/laravel/framework/pull/51546 [11.x] Detect Cockroach DB connection loss by @saschaglo in https://github.com/laravel/framework/pull/51559 [11.x] Fix type tests by @stayallive in https://github.com/laravel/framework/pull/51558 [11.x] Add withoutDelay() to the Queueable trait by @KennedyTedesco in https://github.com/laravel/framework/pull/51555 [11.x] Add an option to remove the original environment file after encrypting by @riasvdv in https://github.com/laravel/framework/pull/51556 [10.x] - Fix batch list loading in Horizon when serialization error by @jeffortegad in https://github.com/laravel/framework/pull/51551 [10.x] Fixes explicit route binding with BackedEnum by @CAAHS in https://github.com/laravel/framework/pull/51586 [11.x] Add Macroable to PendingCommand by @PerryvanderMeer in https://github.com/laravel/framework/pull/51572 [11.x] Improves errors by @nunomaduro in https://github.com/laravel/framework/pull/51261 [11.x] Add RELEASE.md to .gitattributes by @Jubeki in https://github.com/laravel/framework/pull/51598 [11.x] Fixes exception rendering by @nunomaduro in https://github.com/laravel/framework/pull/51587 The post A New Minimal Default Exception Page With Dark Mode Support in Laravel 11.9 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

New Array Find Functions in PHP 8.4
New Array Find Functions in PHP 8.4

Four new array functions are coming to PHP 8.4 which are helper functions for checking an array for the existence of elements matching a specific condition. The new functions are: array_find() array_find_key() array_any() array_all() The array_find() Function The array_find($array, $callback) function returns the first element for which the $callback returns true: $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; array_find($array, function (string $value) { return strlen($value) > 4; }); // string(5) "goose" array_find($array, function (string $value) { return str_starts_with($value, 'f'); }); // null // Find the first animal where the array key is the first symbol of the animal. array_find($array, function (string $value, $key) { return $value[0] === $key; }); Using Laravel's Arr or Collection you can get equivalent functionality with the first() method in combination with a closure: use Illuminate\Support\Arr; use Illuminate\Support\Collection; $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; new Collection($array) ->first(fn ($value) => strlen($value) > 4); // goose Arr::first( $array, fn ($value) => str_starts_with($value, 'f') ); // null new Collection($array) ->first(fn ($value, $key) => $value[0] === $key); // cow Note that we are demonstrating class instantiation without extra parenthesis, which should also be in PHP 8.4. The array_find_key() Function The array_find_key($array, $callback) function returns the key of the first element for which the $callback returns true. Like array_find(), it returns null if no matching element is found: $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; array_find_key($array, function (string $value) { return strlen($value) > 4; }); // string(1) "e" array_find_key($array, function (string $value) { return str_starts_with($value, 'f'); }); // null array_find_key($array, function (string $value, $key) { return $value[0] === $key; }); // string(1) "c" The RFC implementation for this function looks like the following: function array_find_key(array $array, callable $callback): mixed { foreach ($array as $key => $value) { if ($callback($value, $key)) { return $key; } } Β  return null; } Using Laravel's Collection, you can get functionality similar to the search() method in combination with a closure. However, search() returns false if the item is not found, not null: use Illuminate\Support\Arr; use Illuminate\Support\Collection; new Collection($array)->search(function (string $value) { return strlen($value) > 4; }); // string(1) "e" new Collection($array)->search(function (string $value) { return str_starts_with($value, 'f'); }); // false new Collection($array)->search(function (string $value, $key) { return $value[0] === $key; }); // string(1) "c" The array_any() and array_all() Functions The second part of the RFC (and a separate 2/3 vote) includes the array_any() and array_all() functions. You can use these functions if any of the items in array return true for array_any() and if all of the items in an array return true for array_all(), respectively. $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; // Check, if any animal name is longer than 5 letters. array_any($array, function (string $value) { return strlen($value) > 5; }); // bool(true) // Check, if any animal name is shorter than 3 letters. array_any($array, function (string $value) { return strlen($value) < 3; }); // bool(false) // Check, if all animal names are shorter than 12 letters. array_all($array, function (string $value) { return strlen($value) < 12; }); // bool(true) Β  // Check, if all animal names are longer than 5 letters. array_all($array, function (string $value) { return strlen($value) > 5; }); // bool(false) Learn More You can read all the details about this change in the RFC. This feature drops in PHP 8.4. The implementation for these functions can be found on GitHub. The post New Array Find Functions in PHP 8.4 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

The Ability to Prevent Destructive Commands From Running is Coming to Laravel 11
The Ability to Prevent Destructive Commands From Running is Coming to Laravel 11

In an upcoming Laravel release, you can prevent commands like database migrations from accidentally running in production environments. The next Laravel release should include the ability to use a framework Prohibitable trait to stop commands from running: use Illuminate\Console\Command; use Illuminate\Console\Prohibitable; class SomeDestructiveCommand extends Command { use Prohibitable; } // SomeDestructiveCommand::prohibit($this->app->isProduction()); The Laravel framework includes some database commands that include the Prohibitable trait, such as db:wipe, migrate:fresh, migrate:refresh, and migrate:reset: public function boot(): void { FreshCommand::prohibit(); RefreshCommand::prohibit(); ResetCommand::prohibit(); WipeCommand::prohibit(); } Using the DB Facade, you can prohibit destructive database commands built into Laravel: // Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset DB::prohibitDestructiveCommands($this->app->isProduction()); The prohibit() method accepts a Boolean argument that defaults to true, and you can conditionally prevent commands from running using whatever logic you need, so that you can still run them in development environments: public function boot(): void { YourCommand::prohibit($this->app->isProduction()); } We don't know when the next version of Laravel 11.x will be released, but we should get this feature in the next release since it's been merged into the 11.x branch. You can learn more about this upcoming feature in the merged Pull Request #51376 by Jason McCreary. The post The Ability to Prevent Destructive Commands From Running is Coming to Laravel 11 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Configuring Laravel With Additional Environment Files
Configuring Laravel With Additional Environment Files

In Laravel, you can configure additional environment files that will load instead of the .env file. This feature is helpful for testing, where you can load a .env.testing environment file instead of the default. You typically don't need to reach for this feature, but it's nice to know that by setting the APP_ENV environment variable, Laravel can detect custom configurations. CLI Example The most straightforward example of this feature is using a different environment file when running Laravel with the Artisan console or even the phpunit CLI. Using the Artisan command, you can also use a different .env file using the --env flag or defining an APP_ENV. For example, running the following, Laravel will look for .env.demo: # Set up `.env.demo` cp .env .env.demo echo "\nEXAMPLE_SETTING=demo" >> .env.demo # Use the `demo` env php artisan tinker --env=demo # Or set APP_ENV APP_ENV=demo php artisan tinker If found, Laravel won't load the .env file but instead load .env.demo: Example using .env.demo instead of .env Using .env.testing for PHPUnit Tests Building on what we know about the Laravel framework loading a specific ENV file if it exists, running Laravel feature tests in PHPUnit will use the .env file by default. Using .env for tests and local development could quickly cause issues such as configuring a separate database for testing. You could define database connection details in phpunit.xml, but let's also look at setting them in .env.testing. PHPUnit defines an APP_ENV environment variable in phpunit.xml, which means that Laravel looks for a .env.testing file when bootstrapping Feature tests because the phpunit.xml file defines APP_ENV before the Laravel framework gets bootstrapped in Feature tests: <env name="APP_ENV" value="testing"/> That means we can copy the stock .env file to .env.testing and avoid mixing the two files during testing: cp .env .env.testing echo "\nEXAMPLE_SETTING=testing" >> .env.testing You can configure environment variables in phpunit.xml. However, I like using the .env.testing file to ensure a clean environment specifically for testing. It's also up to you whether you version control .env.testing or ignore it in .gitignore. After copying the .env file, you can verify that .env.testing is loaded by adding the following to a test in your tests/Feature folder. Tests in the tests/Unit folder won't bootstrap the Laravel framework: /** * A basic test example. */ public function test_the_application_returns_a_successful_response(): void { logger('Which environment file is Laravel using?', [ 'file' => $this->app->environmentFile() ]); $response = $this->get('/'); $response->assertStatus(200); } When I run phpunit, I get the following log confirming that I'm using the .env.testing file: [2024-05-24 00:22:42] testing.DEBUG: Which environment file is Laravel using? {"file":".env.testing"} If you ignore this file in your VCS, you could add an example file .env.testing.example with your team's conventions or let them decide how to configure tests locally. I recommend setting system-level environment variables in CI to configure things like test databases. Check out the Laravel Documentation for more details on environment configuration. If you're curious how this works at the framework level, check out the setEnvironmentFilePath method and checkForSpecificEnvironmentFile in the Laravel framework source code. The post Configuring Laravel With Additional Environment Files appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

New Proposed Array Find Functions in PHP 8.4
New Proposed Array Find Functions in PHP 8.4

Four new array functions are likely coming to PHP 8.4 that are still in the RFC voting stage. We're encouraged that the voting is already 100% "yes" votes thus far, with voting ending May 29, 2024. While the RFC acceptance of these functions is pending, it seems likely that these functions are coming to PHP 8.4: array_find() array_find_key() array_any() array_all() The array_find() Function The array_find($array, $callback) function returns the first element for which the $callback returns true: $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; array_find($array, function (string $value) { return strlen($value) > 4; }); // string(5) "goose" array_find($array, function (string $value) { return str_starts_with($value, 'f'); }); // null // Find the first animal where the array key is the first symbol of the animal. array_find($array, function (string $value, $key) { return $value[0] === $key; }); Using Laravel's Arr or Collection you can get equivalent functionality with the first() method in combination with a closure: use Illuminate\Support\Arr; use Illuminate\Support\Collection; $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; new Collection($array) ->first(fn ($value) => strlen($value) > 4); // goose Arr::first( $array, fn ($value) => str_starts_with($value, 'f') ); // null new Collection($array) ->first(fn ($value, $key) => $value[0] === $key); // cow Note that we are demonstrating class instantiation without extra parenthesis, which should also be in PHP 8.4. The array_find_key() Function The array_find_key($array, $callback) function returns the key of the first element for which the $callback returns true. Like array_find(), it returns null if no matching element is found: $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; array_find_key($array, function (string $value) { return strlen($value) > 4; }); // string(1) "e" array_find_key($array, function (string $value) { return str_starts_with($value, 'f'); }); // null array_find_key($array, function (string $value, $key) { return $value[0] === $key; }); // string(1) "c" The RFC implementation for this function looks like the following: function array_find_key(array $array, callable $callback): mixed { foreach ($array as $key => $value) { if ($callback($value, $key)) { return $key; } } Β  return null; } Using Laravel's Collection, you can get functionality similar to the search() method in combination with a closure. However, search() returns false if the item is not found, not null: use Illuminate\Support\Arr; use Illuminate\Support\Collection; new Collection($array)->search(function (string $value) { return strlen($value) > 4; }); // string(1) "e" new Collection($array)->search(function (string $value) { return str_starts_with($value, 'f'); }); // false new Collection($array)->search(function (string $value, $key) { return $value[0] === $key; }); // string(1) "c" The array_any() and array_all() Functions The second part of the RFC (and a separate 2/3 vote) includes the array_any() and array_all() functions. You can use these functions if any of the items in array return true for array_any() and if all of the items in an array return true for array_all(), respectively. $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; // Check, if any animal name is longer than 5 letters. array_any($array, function (string $value) { return strlen($value) > 5; }); // bool(true) // Check, if any animal name is shorter than 3 letters. array_any($array, function (string $value) { return strlen($value) < 3; }); // bool(false) // Check, if all animal names are shorter than 12 letters. array_all($array, function (string $value) { return strlen($value) < 12; }); // bool(true) Β  // Check, if all animal names are longer than 5 letters. array_all($array, function (string $value) { return strlen($value) > 5; }); // bool(false) Learn More You can read all the details about this proposed change in the RFC. This feature likely drops in PHP 8.4. The implementation for these functions is currently in draft and can be found on GitHub. The post New Proposed Array Find Functions in PHP 8.4 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8
A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8

This week, the Laravel team released v11.8, with a new validation rule, the ability to fail a command outside the handle() method, create a view during make:mail, and more. Show Events in the model:show Command Wendell Adriel contributed an Events section to the model:show command that displays any events defined on the $dispatchesEvents property: New contains Validation Rule Andrew Brown contributed a contains validation rule that checks to make sure expected values are included in the given array of input: return [ 'allowed_ips' => ['present', 'nullable', 'array', 'contains:' . $request->ip()], 'allowed_ips.*' => ['required', 'ip'], ]; In the PR's description, this example ensures that the user's IP is in the allowed_ips array. You can also pass multiple parameters, which would require that all parameters are present in the array of data. See Pull Request #51348 for more details. Ability to Manually Fail a Command Len Woodward contributed the ability to manually fail an Artisan command outside of the handle() method. Like the Queue's $this->fail() convenience method, commands can now manually fail a job: public function handle() { $this->trigger_failure(); } protected function trigger_failure() { $this->fail('Whoops!'); } See Pull Request #51435 for implementation details and examples of how this method could be useful over a few other existing approaches to failing early in a command. Create a Blade View With make:mail Ryan Chandler contributed a --view flag to the make:mail command that will create an empty Blade file and configure the created Mailable to use it by default. It works the same way as the existing --markdown option and saves the manual step of creating and wiring up a Blade mail template. php artisan make:mail OrderShipped --view=mail.orders.shipped Release notes You can see the complete list of new features and updates below and the diff between 11.7.0 and 11.8.0 on GitHub. The following release notes are directly from the changelog: v11.8.0 [11.x] Update PendingRequest.php by @foremtehan in https://github.com/laravel/framework/pull/51338 Add unshift method to Collection by @timkelty in https://github.com/laravel/framework/pull/51344 [11.x] Synchronizing cache configuration file with updated laravel v11.0.7 by @dvlpr91 in https://github.com/laravel/framework/pull/51336 [11.x] Utilize null-safe operator instead of conditional check by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51328 [11.x] Add the events to be displayed on the model:show command by @WendellAdriel in https://github.com/laravel/framework/pull/51324 [11.x] fix: remove use of Redis::COMPRESSION_ZSTD_MIN by @calebdw in https://github.com/laravel/framework/pull/51346 [10.x] Backport: Fix SesV2Transport to use correct EmailTags argument by @Tietew in https://github.com/laravel/framework/pull/51352 [11.x] feat: use phpredis 6 in ci by @calebdw in https://github.com/laravel/framework/pull/51347 [11.x] create new "has" validation rule by @browner12 in https://github.com/laravel/framework/pull/51348 [11.x] Add support for previous apps keys in signed URL verification by @Krisell in https://github.com/laravel/framework/pull/51222 [11.x] Allow setting exit code in migrate:status --pending by @brecht-vermeersch in https://github.com/laravel/framework/pull/51341 [11.x] Fix array rule typehint by @erik-perri in https://github.com/laravel/framework/pull/51372 [11.x] Test Improvements by @crynobone in https://github.com/laravel/framework/pull/51365 [10.x] Fix PHPDoc typo by @staudenmeir in https://github.com/laravel/framework/pull/51390 [11.x] Fix return type hint of resolveRouteBindingQuery by @philbates35 in https://github.com/laravel/framework/pull/51392 [11.x] Allow adding array or string for web and api routes in bootstrap/app.php by @mrthito in https://github.com/laravel/framework/pull/51356 [ 11.x ] Adds ability to manually fail a command from outside the handle() method by @ProjektGopher in https://github.com/laravel/framework/pull/51435 [10.x] Fix apa on non ASCII characters by @faissaloux in https://github.com/laravel/framework/pull/51428 [11.x] Compare lowercased column names in getColumnType by @chady in https://github.com/laravel/framework/pull/51431 [11.x] Use contracts instead of concrete type for resolveRouteBindingQuery() by @crynobone in https://github.com/laravel/framework/pull/51425 [11.x] Set the value of $this in macro closures by @simonwelsh in https://github.com/laravel/framework/pull/51401 [11.x] Add missing roundrobin transport driver config by @u01jmg3 in https://github.com/laravel/framework/pull/51400 [11.x] Remove unused namespace by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51436 [11.x] Fixes doc block in Connector.php by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51440 [10.x] Fixes view engine resolvers leaking memory by @nunomaduro in https://github.com/laravel/framework/pull/51450 [11.x] Add some tests to SupportStrTest by @saMahmoudzadeh in https://github.com/laravel/framework/pull/51437 [11.x] Add isCurrentlyOwnedBy function to lock by @gazben in https://github.com/laravel/framework/pull/51393 [11.x] Collection average/avg optimization by @bert-w in https://github.com/laravel/framework/pull/51512 [11.x] Introduce MixManifestNotFoundException for handling missing Mix manifests by @xurshudyan in https://github.com/laravel/framework/pull/51502 [11.x] MailMakeCommand: Add new --view option by @ryangjchandler in https://github.com/laravel/framework/pull/51411 [11.x] Replace all backed enums with values when building URLs by @stefanvdlugt in https://github.com/laravel/framework/pull/51524 [10.x] Do not use app() Foundation helper on ViewServiceProvider by @rodrigopedra in https://github.com/laravel/framework/pull/51522 Fixes explicit route binding with BackedEnum by @crynobone in https://github.com/laravel/framework/pull/51525 [11.x] Add query method to UrlGenerator contract docblock by @hjanos-bc in https://github.com/laravel/framework/pull/51515 The post A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

A Look at What's Coming to PHP 8.4
A Look at What's Coming to PHP 8.4

PHP 8.4 is coming soon and in this post let's look at what has been announced so far, and what new features you might expect. When is PHP 8.4 scheduled to be released? PHP 8.4 is scheduled to be released on November 21, 2024. Before the release it will feature 6 months of pre-release phases going from Alphas, to Betas, to Release Candidates, and then the official release. New Array Find Functions PHP 8.4 will come with new array find functions that include: array_find() array_find_key() array_any() array_all() See our post on the PHP 8.4 Array Find Functions PHP Property Hooks Property hooks are inspired by languages like Kotlin, C#, and Swift, and the syntax includes two syntax variants that resemble short and multi-line closures: class User implements Named { private bool $isModified = false; public function __construct( private string $first, private string $last ) {} public string $fullName { // Override the "read" action with arbitrary logic. get => $this->first . " " . $this->last; // Override the "write" action with arbitrary logic. set { [$this->first, $this->last] = explode(' ', $value, 2); $this->isModified = true; } } } Property hooks will help remove boilerplate of property getters and setters, allowing a property to define access and updates using hooks. Check out our post for more details: Property Hooks in PHP 8.4. new MyClass()->method() without parentheses Since member access during instantiation was introduced, you must wrap the new MyClass() call in parentheses, or you'll get a parse error. The proposed syntax would allow you to access constants, properties, and methods without the extra parentheses: // Wrapping parentheses are required to access class members $request = (new Request())->withMethod('GET')->withUri('/hello-world'); Β  // PHP Parse error (<= PHP 8.3): syntax error, unexpected token "->" $request = new Request()->withMethod('GET')->withUri('/hello-world'); This update fixes papercut that makes working with class member access simpler, not having to add surrounding parentheses or using a static constructor method. This syntax change also puts PHP more in alignment with other C languages like Java, C#, and TypeScript, which don't require surrounded parentheses. Check out our post for more details: Class Instantiation Without Extra Parenthesis in PHP 8.4. Learn More You can follow the PHP 8.4 Preparation Tasks on the wiki. The post A Look at What's Coming to PHP 8.4 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Is class instantiation without extra parenthesis coming to PHP 8.4?
Is class instantiation without extra parenthesis coming to PHP 8.4?

The RFC for omitting parentheses around the new expression is likely coming to PHP 8.4. This RFC is currently in the voting phase, with 21 "yes" and 3 "no" votes. Voting ends May 24th, so there's still a chance the 2/3 vote could fail, but optimistically, it looks like it's heading in the right direction. Since member access during instantiation was introduced, you must wrap the new MyClass() call in parentheses, or you'll get a parse error. The proposed syntax would allow you to access constants, properties, and methods without the extra parentheses: class Request implements Psr\Http\Message\RequestInterface { // ... } Β  // Valid $request = (new Request())->withMethod('GET')->withUri('/hello-world'); Β  // PHP Parse error: syntax error, unexpected token "->" $request = new Request()->withMethod('GET')->withUri('/hello-world'); Here are some common examples you'll probably use with this feature (the RFC has more examples): var_dump( new MyClass()::CONSTANT, // string(8) "constant" new MyClass()::$staticProperty, // string(14) "staticProperty" new MyClass()::staticMethod(), // string(12) "staticMethod" new MyClass()->property, // string(8) "property" new MyClass()->method(), // string(6) "method" new MyClass()(), // string(8) "__invoke" ); You can read all the details about this proposed change in the RFC. This feature likely drops in PHP 8.4. The implementation looks to be code-complete (not approved and merged yet) and can be found on GitHub. The post Is class instantiation without extra parenthesis coming to PHP 8.4? appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Validation Errors Card for Laravel Pulse
Validation Errors Card for Laravel Pulse

The Validation Errors Card for Laravel Pulse shows useful metrics for validation errors impacting users. What I love about this Pulse card is that it shows how real users experience and interact with validation errors in your app. Validation Errors Card in Laravel Pulse This package includes the following main features with the initial release: Supports multiple error bags Supports session based validation errors Supports API validation errors Support Inertia validation errors Fallback for undetectable validation errors (based on 422 response status) To get started with this package you can install it via Composer: composer require timacdonald/pulse-validation-errors Then you can configure it as a recorder in the pulse.php config file and then add the card your Pulse dashboard: return [ // ... 'recorders' => [ TiMacDonald\Pulse\Recorders\ValidationErrors::class => [ 'enabled' => env('PULSE_VALIDATION_ERRORS_ENABLED', true), 'sample_rate' => env('PULSE_VALIDATION_ERRORS_SAMPLE_RATE', 1), 'capture_messages' => true, 'ignore' => [ // '#^/login$#', // '#^/register$#', // '#^/forgot-password$#', ], ], // ... ], ]; // Add to your Pulse dashboard view // <livewire:pulse.validation-errors cols="8" rows="4" /> You can learn more about this package, get full installation instructions, and view the source code on GitHub. To get started with Pulse, follow the setup guide in the Laravel Documentation. The post Validation Errors Card for Laravel Pulse appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.