Introducing Laravel Quizzes! Play now

What's New in Laravel: 8.58 - 8.68

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.

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

$user->update(); // return bool

try {
    $user->updateOrFail();
} catch (Exception $e) {
    // Exception thrown
}

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

$user->delete(); // return bool

try {
    $user->deleteOrFail();
} catch (Exception $e) {
    // Exception thrown
}

valueOrFail() method, which gets a single column's value from the first result of a query or throws an exception: - PR

// Before:
$votes = User::where('name', 'John')->firstOrFail('votes')->votes;
 
// Now:
$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

$feedback = Feedback::query()
    ->where('subject_type', $model->getMorphClass())
    ->where('subject_id', $model->getKey())
    ->get();

// turns into
$feedback = Feedback::whereMorphedTo('subject', $model)->get();

Added whereBelongsTo() Eloquent builder method: - PR

$query->where('author_id', $author->id)

// turns into
$query->whereBelongsTo($author)

Validation

prohibits: Allows you to prohibit a field from being passed if another is also passed. - PR

Validator::validate([
    'email' => 'example@example.com', 
    'emails' => ['example@example.com', 'other.example@example.com']
], [
    'email' => 'prohibits:emails'
]);

merge() method to merge items to validated inputs: - PR

$validator = Validator::make(['name' => 'Taylor'], ['name' => 'required']);

$validator->safe()->merge(['role' => 'Admin']);

User::create($validator->safe()->all());

Multiple Date Formats - PR

public function rules()
{
    return [
        'date' => 'date_format:Y-m-d,m-d',
    ];
}

Requests

Using the collect method, you may retrieve all of the incoming request's input data as a collection: - Docs

$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:

{{-- components/accordion/index.blade.php --}}
<x-accordion>
    {{-- components/accordion/item.blade.php --}}
    <x-accordion.item>
        {{-- ... --}}
    </x-accordion.item>
</x-accordion>

The @aware Blade Directive -Docs

The new @aware directive allows child components to easily access parent component data when needed:

<!-- Example usage -->
<x-menu color="purple">
    <x-menu.item>...</x-menu.item>
    <x-menu.item>...</x-menu.item>
</x-menu>
 
<!--
Implementation
/resources/views/components/menu/index.blade.php
-->
 
@aware(['color' => 'gray'])
 
<li {{ $attributes->merge(['class' => 'text-'.$color.'-800']) }}>
    {{ $slot }}
</li>

Events

Maintenance Mode Events - PR

use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Events\MaintenanceModeDisabled;
 
Event::dispatch(MaintenanceModeEnabled::class);
Event::dispatch(MaintenanceModeDisabled::class);

Testing

assertNotSoftDeleted which asserts that a given record has not been soft deleted: - PR

$this->assertNotSoftDeleted($model);
$this->assertNotSoftDeleted('posts', ['id' => 1]);

Other

  • stripTags() string method - PR
  • lang_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
  • createOneQuietly and createManyQuietly methods 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
Anthony Rappa

By Anthony Rappa

Hello! I'm a full stack developer from Long Island, New York. Working mainly with Laravel, Tailwind, Livewire, and Alpine.js (TALL Stack). I share everything I know about these tools and more, as well as any useful resources I find from the community. You can find me on GitHub and LinkedIn.