Posts

Since 2024
Anonymous Event Broadcasting in Laravel 11.5
Anonymous Event Broadcasting in Laravel 11.5

This week, the Laravel team released v11.5, with anonymous event broadcasting, Blade performance improvements, generating URLs with query parameters, and more. Anonymous Event Broadcasting Joe Dixon contributed anonymous broadcasts in Laravel for real-time applications using Laravel Echo: Sometimes you may wish to broadcast an ad-hoc event. An ad-hoc event is one where you don't need to hook into it anywhere else in your application. You just want to notify the frontend of something. For this, you don't want to go to the trouble of creating a brand new event, you just want to fire off a message. For this, we can use an anonymous broadcast using the Broadcast facade, which can be as simple as: Broadcast::on('my-channel')->send(); // You may dispatch to multiple channels at the same time: Broadcast::on([ 'my-channel', new PrivateChannel('my-channel'), 'presence-my-channel' )->send(); // Broadcast the anonymous event on a private or presence channel Broadcast::private('my-channel')->send(); Broadcast::presence('my-channel')->send(); To learn more about anonymous event broadcasting in Laravel, check out Laravel's Documentation. Blade Performance Improvements Taylor Otwell shared a thought about supercharging Blade component rendering performance. Two pull requests were accepted and merged as part of Laravel 11.5, which collectively improved Blade rendering by 20%: Supercharge Blade by Christopher Pitt Blade Component Loop Speed Improvement by Lonny Kapelushnik Ability to Generate URLs With Query Params Steve Bauman contributed the ability to generate URLs with query parameters via the new query() method: // http://localhost/products?sort=-name url()->query('products', ['sort' => '-name']); // http://localhost/products?columns[0]=name&columns[1]=price&columns[2]=quantity url()->query('products', ['columns' => ['name', 'price', 'quantity']]); // Overiding parameters: // http://localhost/products?sort=-price url()->query('products?sort=-name', ['sort' => '-price']); // Appending parameters // http://localhost/products?sort=-name&search=samsung url()->query('products?sort=-name', ['search' => 'samsung']); Add a Default Namespace for make:trait and make:interface @milwad-dev contributed a default namespace for make:trait and make:interface, which will create these classes in the following paths if they exist: Interfaces: App\Contracts App\Interfaces Traits: App\Concerns App\Traits If any of those folders exist in your project, Laravel will create the file in that namespace. For example, App\Contracts would take precedence over App\Interfaces. Lastly, the file is created in the App namespace directly if either of the directories are not found. Release notes You can see the complete list of new features and updates below and the diff between 11.4.0 and 11.5.0 on GitHub. The following release notes are directly from the changelog: v11.5.0 [11.x] Add namespace for make:trait and make:interface command by @milwad-dev in https://github.com/laravel/framework/pull/51083 [11.x] Ability to generate URL's with query params by @stevebauman in https://github.com/laravel/framework/pull/51075 [11.x] Adds anonymous broadcasting by @joedixon in https://github.com/laravel/framework/pull/51082 [10.x] Binding order is incorrect when using cursor paginate with multiple unions with a where by @thijsvdanker in https://github.com/laravel/framework/pull/50884 [10.x] Fix cursor paginate with union and column alias by @thijsvdanker in https://github.com/laravel/framework/pull/50882 [11.x] Fix typo in tests by @milwad-dev in https://github.com/laravel/framework/pull/51093 Fix argument type in Cache\Store by @GromNaN in https://github.com/laravel/framework/pull/51100 Correct comment's grammatical and semantic errors by @javadihugo in https://github.com/laravel/framework/pull/51101 [11.x] Replace matches typehint fix by @henzeb in https://github.com/laravel/framework/pull/51095 [11.x] Exclude laravel_through_key when replicating model, fixes #51097 by @levu42 in https://github.com/laravel/framework/pull/51098 [11.x] Add enum types to static Rule methods by @erik-perri in https://github.com/laravel/framework/pull/51090 [11.x] Add decrement method to the rate limiter class by @AlexJump24 in https://github.com/laravel/framework/pull/51102 [11.x] Remove dead code by @michaelnabil230 in https://github.com/laravel/framework/pull/51106 [11.x] Fix support for other hashing implementations when using hashed cast by @j3j5 in https://github.com/laravel/framework/pull/51112 Revert "[11.x] Adds support for int backed enums to implicit Enum route binding" by @driesvints in https://github.com/laravel/framework/pull/51119 [11.x] Add support for enums in whereIn route constraints by @osbre in https://github.com/laravel/framework/pull/51121 Clarify that \Illuminate\Http\Request::replace replace all input values by @treyssatvincent in https://github.com/laravel/framework/pull/51123 [11.x] Fix db:show's --counts option by @xuchunyang in https://github.com/laravel/framework/pull/51140 Update RuntimeException message when no data has been found by @mikemeijer in https://github.com/laravel/framework/pull/51133 [11] Update DetectsLostConnections.php by @it-can in https://github.com/laravel/framework/pull/51127 [11.x] Reset connection after migrate for FreshCommand by @driesvints in https://github.com/laravel/framework/pull/51167 [10.x] Address Null Parameter Deprecations in UrlGenerator by @aldobarr in https://github.com/laravel/framework/pull/51148 [11.x] Provide context for NestedRules by @imahmood in https://github.com/laravel/framework/pull/51160 [11.x] Fix renaming columns with NULL as default on legacy MariaDB/MySQL by @hafezdivandari in https://github.com/laravel/framework/pull/51177 [11.x] Supercharge Blade by @assertchris in https://github.com/laravel/framework/pull/51143 [11.x] Allow implicit binding to have optional backed enums by @Neol3108 in https://github.com/laravel/framework/pull/51178 [11.x] Blade Component Loop Speed Improvement by @lonnylot in https://github.com/laravel/framework/pull/51158 [11.x] Fix normalizedNameCache by @Jubeki in https://github.com/laravel/framework/pull/51185 [11.x] GenericUser use getAuthPasswordName instead of hardcoded column name by @Daniel-H123 in https://github.com/laravel/framework/pull/51186 The post Anonymous Event Broadcasting in Laravel 11.5 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Microsoft Clarity Integration for Laravel
Microsoft Clarity Integration for Laravel

The clarity-laravel package lets you easily integrate Microsoft Clarity into your Laravel application. I wasn't familiar with Clarity before seeing this package—it's a GDPR and CCPA-ready product that you embed in your application, and it can capture how people use your site: Heatmaps with Microsoft Clarity The main features that Clarity offers your application include: Heatmaps Session recordings Insights Google Analytics integration Integration is easy with this package: you set up a few environment variables and include the package's Blade component in your application's layout file: <head> <x-clarity::script /> </head> This package will enable Clarity based on the CLARITY_ENABLED environment variable value in the clarity.php configuration file. If setting the environment variable isn't flexible enough, you can set the :enabled property on the component with a variable boolean value that you define: <x-clarity::script :enabled="$enabled" /> While you could easily integrate the Clarity embed code in your application directly, this package takes care of it for you, and you can start collecting data in minutes. You can learn more about this package, get full installation instructions, and view the source code on GitHub. You can learn more about Clarity from the Microsoft Clarity documentation. You can also see a live demo The post Microsoft Clarity Integration for Laravel appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Apply Dynamic Filters to Eloquent Models with the Filterable Package
Apply Dynamic Filters to Eloquent Models with the Filterable Package

Filterable is a Laravel package by Jerome Thayananthajothy that enhances Laravel queries with adaptable, customizable filters and intelligent caching to improve both performance and functionality. The main features of this package include: Dynamic Filtering: Apply filters based on request parameters with ease. Caching: Improve performance by caching query results. User-specific Filtering: Easily implement filters that depend on the authenticated user. Custom Filter Methods: Extend the class to add your own filter methods. Defining Filter classes is at the center of this package, where you can create methods that can apply filtering to Eloquent queries. The package includes a make:filter Artisan command to generate a filter in your app's App\Filters namespace. Here's an example of a filter from the package's README: namespace App\Filters; use Filterable\Filter; use Illuminate\Database\Eloquent\Builder; class PostFilter extends Filter { protected array $filters = ['status', 'category']; protected function status(string $value): Builder { return $this->builder->where('status', $value); } protected function category(int $value): Builder { return $this->builder->where('category_id', $value); } } Given a PostFilter, you can utilize this class in a controller with the Post model to filter models based on the HTTP query params: public function index(Request $request, PostFilter $filter) { // i.e., /posts?status=active&category_id=2 $query = Post::filter($filter); $posts = $request->has('paginate') ? $query->paginate($request->query('per_page', 20)) : $query->get(); return response()->json($posts); } You can learn more about this package, get full installation instructions, and view the source code on GitHub. The post Apply Dynamic Filters to Eloquent Models with the Filterable Package appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Property Hooks Get Closer to Becoming a Reality in PHP 8.4
Property Hooks Get Closer to Becoming a Reality in PHP 8.4

The Property Hooks RFC passed a significant milestone, getting an overwhelmingly positive 34 "yes" votes and only 1 "no" vote. That's well above the required 2/3 majority required to pass. What are property hooks in PHP? Here's the proposal summary from the RFC: Developers often use methods to wrap and guard access to object properties. There are several highly common patterns for such logic, which in practice may be verbose to implement repeatedly. Alternatively, developers may use __get and __set to intercept reads and writes generically, but that is a sledge-hammer approach that intercepts all undefined (and some defined) properties unconditionally. Property hooks provide a more targeted, purpose-built tool for common property interactions... This RFC introduces two “hooks” to override the default “get” and “set” behavior of a property. Although not included in this initial version, the design includes the ability to support more hooks in the future. 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; } } } The syntax doesn't require that both hooks always be defined together; in fact, here's an example of only defining set from the RFC: class User { public string $name { set { if (strlen($value) === 0) { throw new ValueError("Name must be non-empty"); } $this->name = $value; } }   public function __construct(string $name) { $this->name = $name; } } You can read all the details about Property Hooks in PHP in the RFC. This feature looks likely to drop in PHP 8.4. The implementation is already a draft pull request if you want to see the discussion and progress of this feature. The post Property Hooks Get Closer to Becoming a Reality in PHP 8.4 appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

Asserting Exceptions in Laravel Tests
Asserting Exceptions in Laravel Tests

Laravel's recent release of Laravel 11.4 introduced the Exceptions facade, which adds conveniences around asserting exceptions in Laravel's exception handler. Before this release, I would typically use the $this->withoutExceptionHandling() to assert that a specific exception happened during an HTTP test: use App\Exceptions\WelcomeException; $this->withoutExceptionHandling(); try { $this->get('/'); } catch (WelcomeException $e) { $this->assertEquals('Woops, there was an issue with your request!', $e->getMessage()); return; } $this->fail(sprintf('The expected "%s" exception was not thrown.', WelcomeException::class)); When you expect a request to not throw any exceptions, using withoutExceptionHandling cuts out the middleman when you're debugging why an error is happening when you don't expect it. The above code is tedious to write, because it manually captures the exception, makes assertions about the exception, and calls return to avoid the manual $this->fail() call. The manual failure will catch situations when the test doesn't throw an exception when expected. If $this->fail() is called in the above scenario, here's what the output would look like: $ phpunit There was 1 failure: 1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response The expected "App\Exceptions\WelcomeException" exception was not thrown. /app/tests/Feature/ExampleTest.php:33 Laravel's Exceptions Facade Let's look at how the new Exceptions facade can simplify our test; the first example, rewritten, would look as follows: use Illuminate\Support\Facades\Exceptions; Exceptions::fake(); $this->get('/'); Exceptions::assertReported(function (WelcomeException $e): bool { return $e->getMessage() === "Woops, there was an issue with your request!"; }); Using the Exceptions facade gives us the bonus of not having to capture an exception to assert things manually. Said another way, the test can keep Laravel's exception handler in place but still be able to assert exceptions that happened during a request. If we want to be sure that a test doesn't throw a specific exception or doesn't throw any exceptions, period, the new facade has our back: Exceptions::assertNotReported(WelcomeException::class);   Exceptions::assertNothingReported(); If the exception handler does not report the WelcomeException, the test output would give us a nicely formatted message: $ phpunit There was 1 failure: 1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response The expected [App\Exceptions\WelcomeException] exception was not reported. While there may be times when you don't want to fake Laravel's exception handler, when testing edge cases, the new Exceptions facade is tremendously helpful and cleans up our code: Exceptions::assertReported(WelcomeException::class); Exceptions::assertReportedCount($count); Exceptions::assertNotReported(WelcomeException::class); Exceptions::assertNothingReported(); Exceptions::throwFirstReported(); To learn more about the Exceptions facade, check out Laravel’s documentation. The post Asserting Exceptions in Laravel Tests appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.