Posts

Since 2024
DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel
DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel

The DirectoryTree Authorization package by Steve Bauman is an easy, native role and permission management system for Laravel. If you need a super simple native feeling Laravel Authorization package, this one may tickle your fancyhttps://t.co/Xiu4Ty0IkR— Steve (@ste_bau) April 23, 2024 <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> It works with Laravel's Gate and authorization methods out of the box, and offers the following lightweight API to manage roles and permissions: use DirectoryTree\Authorization\Permission; use DirectoryTree\Authorization\Role; $createUsers = Permission::create([ 'name' => 'users.create', 'label' => 'Create Users', ]); $admin = Role::create([ 'name' => 'administrator', 'label' => 'Admin', ]); // Grant the permission to a role $admin->permissions()->save($createUsers); // Assign the role to a user $user->roles()->save($admin); // `can()` method usage in PHP: Auth::user()->can('users.create'); // Using Laravel's `Gate`: Gate::allows('users.create'); // Using Laravel's `@can()` directive: @can('users.create') <!-- This user can create other users. --> @endcan The above code snippet doesn't contain every method available—see the readme for usage details on managing roles and permissions with this package, which includes the following main features: Manage User roles and Permissions Create user-specific permissions Checking permissions and roles Caching permissions by default Use with Laravel's native Gate and authorization methods. Permissions are registered in Laravel's Gate by default Includes useful permission and role middleware To get started with this package, check out package on GitHub at directorytree/authorization. The post DirectoryTree Authorization is a Native Role and Permission Management Package for Laravel appeared first on Laravel News. Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.

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.