What's New in Laravel: 8.58 - 8.68
Are you like me and find it impossible to find the time to keep up with the changes in Laravel on a weekly basis? In this series I'll try to roll them up periodically into one post.
Are you like me and find it impossible to find the time to keep up with the changes in Laravel on a weekly basis? In this series I'll try to roll them up periodically into one post.
I'm only going to include features and not patches or fixes, for a full list visit the Laravel release log.
Eloquent
updateOrFail uses saveOrFail to update or throw an exception instead of just returning a bool. - PR
1$user->update(); // return bool2 3try {4 $user->updateOrFail();5} catch (Exception $e) {6 // Exception thrown7}
deleteOrFail makes it more convenient to handle delete errors in destroy actions, as it is less verbose than checking the return value of delete(). - PR
1$user->delete(); // return bool2 3try {4 $user->deleteOrFail();5} catch (Exception $e) {6 // Exception thrown7}
valueOrFail() method, which gets a single column's value from the first result of a query or throws an exception: - PR
1// Before:2$votes = User::where('name', 'John')->firstOrFail('votes')->votes;3 4// Now:5$votes = User::where('name', 'John')->valueOrFail('votes');
whereMorphedTo and orWhereMorphedTo are a shortcut for adding a where condition looking for models that are morphed to a specific related model, without the overhead of a whereHas subquery.- PR
1$feedback = Feedback::query()2 ->where('subject_type', $model->getMorphClass())3 ->where('subject_id', $model->getKey())4 ->get();5 6// turns into7$feedback = Feedback::whereMorphedTo('subject', $model)->get();
Added whereBelongsTo() Eloquent builder method: - PR
1$query->where('author_id', $author->id)2 3// turns into4$query->whereBelongsTo($author)
Validation
prohibits: Allows you to prohibit a field from being passed if another is also passed. - PR
1Validator::validate([4], [5 'email' => 'prohibits:emails'6]);
merge() method to merge items to validated inputs: - PR
1$validator = Validator::make(['name' => 'Taylor'], ['name' => 'required']);2 3$validator->safe()->merge(['role' => 'Admin']);4 5User::create($validator->safe()->all());
Multiple Date Formats - PR
1public function rules()2{3 return [4 'date' => 'date_format:Y-m-d,m-d',5 ];6}
Requests
Using the collect method, you may retrieve all of the incoming request's input data as a collection: - Docs
1$input = $request->collect();
Blade
Anonymous Index Blade Component Templates - Docs
Caleb Porzio contributed the ability to use index.blade.php for the default view for an anonymous component pointing to a folder:
1{{-- components/accordion/index.blade.php --}}2<x-accordion>3 {{-- components/accordion/item.blade.php --}}4 <x-accordion.item>5 {{-- ... --}}6 </x-accordion.item>7</x-accordion>
The @aware Blade Directive -Docs
The new @aware directive allows child components to easily access parent component data when needed:
1<!-- Example usage --> 2<x-menu color="purple"> 3 <x-menu.item>...</x-menu.item> 4 <x-menu.item>...</x-menu.item> 5</x-menu> 6 7<!-- 8Implementation 9/resources/views/components/menu/index.blade.php10-->11 12@aware(['color' => 'gray'])13 14<li {{ $attributes->merge(['class' => 'text-'.$color.'-800']) }}>15 {{ $slot }}16</li>
Events
Maintenance Mode Events - PR
1use Illuminate\Foundation\Events\MaintenanceModeEnabled;2use Illuminate\Foundation\Events\MaintenanceModeDisabled;3 4Event::dispatch(MaintenanceModeEnabled::class);5Event::dispatch(MaintenanceModeDisabled::class);
Testing
assertNotSoftDeleted which asserts that a given record has not been soft deleted: - PR
1$this->assertNotSoftDeleted($model);2$this->assertNotSoftDeleted('posts', ['id' => 1]);
Other
stripTags()string method - PRlang_path()helper that will look for the lang directory in both the resources path and a top-level lang folder in the root of a Laravel project - PR- Collections "has any" Method - PR
createOneQuietlyandcreateManyQuietlymethods for model factories that can create one or many models and persist them to the database without model events.- String headline method - PR
- restrictOnUpdate migration method- PR