The Laravel team released v12.7.0 recently, which includes two excellent new features: resource helper functions for models and a whereAttachedTo()
Eloqent method:
Resource Helper Methods for Models
Tim Kunze contributed helper methods that will make generating resource instances fluent using an Eloquent model or collection:
// BeforeUserResource::make(User::find(1));UserResource::collection(User::query()->active()->paginate()); // AfterUser::find(1)->toResource(UserResource::class); User::query()->active()->paginate()->toResourceCollection(UserResource::class);
You can also call toResource()
without any arguments, and the model will guess the resource name. Of course, you can be explicit and pass a resource, but when you use Laravel's conventions, it can be omitted:
User::find(1)->toResource();
See Pull Request #55107 for discussion and implementation details.
whereAttachedTo()
Query Builder Method
Eloquent Jacob Baker-Kretzmar contributed a whereAttachedTo()
Eloquent query builder method to simplify retrieving records "attached" to a model via a BelongsToMany
relationship:
$tags = Tag::where('created_at', '>', now()->subMonth())->get(); // Before$taggedPosts = Post::whereHas('tags', function ($query) use ($tags) { $query->whereKey($tags);})->get(); // After$taggedPosts = Post::whereAttachedTo($tags)->get(); // After with explicit relationship name$taggedPosts = Post::whereAttachedTo($tags, 'tags')->get();
Make the Uri Class Macroable
Similar to many other classes having the Macroable
trait, Richard van Baarsen contributed adding the Macroable
trait to the URI class:
use Illuminate\Support\Uri; Uri::macro('docs', fn () => $this->withPath('docs'));new Uri('https://laravel.com/')->docs(); // https://laravel.com/docs
Release notes
You can see the complete list of new features and updates below and the diff between 12.6.0 and 12.7.0 on GitHub. The following release notes are directly from the changelog:
v12.7.0
- [12.x]
AbstractPaginator
should implementCanBeEscapedWhenCastToString
by @gdebrauwer in https://github.com/laravel/framework/pull/55256 - [12.x] Add
whereAttachedTo()
Eloquent builder method by @bakerkretzmar in https://github.com/laravel/framework/pull/55245 - Make Illuminate\Support\Uri Macroable by @riesjart in https://github.com/laravel/framework/pull/55260
- [12.x] Add resource helper functions to Model/Collections by @TimKunze96 in https://github.com/laravel/framework/pull/55107
- [12.x]: Use char(36) for uuid type on MariaDB < 10.7.0 by @boedah in https://github.com/laravel/framework/pull/55197
- [12.x] Introducing
toArray
toComponentAttributeBag
class by @devajmeireles in https://github.com/laravel/framework/pull/55258