Latest Updates in Laravel 9

Laravel is the most famous web application framework for many years. Because of its easy working module, elegant syntax, and reusability of code helps developers to simplify the process of website development. Common tasks like routing, sessions, caching, and authentication are easily done in Laravel. Laravel offers a powerful tool that is required for large robust applications. The superb use of control container, the migration system, and well-integrated unit testing makes Laravel a perfect application to build a website.

  • Laravel version 9.
  • Release Date: 8th Feb ,2022
  • PHP version 8.
  • Security Updates till 8th Feb, 2024

The latest updates in Laravel 9 that you must know are listed below:

Controller Route Group

The controller group comprises common routes that make the route user friendly

use App\Http\Controllers\PostController;
 
Route::controller(PostController::class)->group(function () {
    Route::get('/posts/{id}', 'show');
    Route::post('/posts', 'store');
});

Inline Blade Templating

Laravel 9 offers a feature called as Inline Blade Templating to convert any blade template string into HTML tags.

use Illuminate\Support\Facades\Blade;
 
return Blade::render('Hi, {{$name}}', ['name' => 'shawn']);

Stub Migrations

In Laravel 9, we can avoid any migration class name collision using the anonymous stub migration feature

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration {

    public function up()
    {
        Schema::table('people', function (Blueprint $table) {
            $table->string('name')->nullable();
        });
    }
};

Improvement in Accessors/Mutators

Model Accessors/ Mutators can be easily defined in Laravel with easily sorted syntax.

Before Laravel 9 syntax

public function getNameAttribute($value)
{
    return strtoupper($value);
}
 
public function setNameAttribute($value)
{
    $this->attributes['name'] = $value;
}

In Laravel 9 syntax

use Illuminate\Database\Eloquent\Casts\Attribute;
 
public function name(): Attribute
{
    return new Attribute(
        get: fn ($value) => strtoupper($value),
        set: fn ($value) => $value,
    );
}

Eloquent Attributes to Enum

Now, we can cast eloquent attributes to enum like eloquent date, number.

use App\Enums\PostStatus;

protected $casts = [
    'status' => PostStatus::class,
];

Full-text Indexes

Laravel 9 offers full text indexes

$table->text('bio')->fullText();

In query

DB::table('users')
    ->whereFullText('bio', 'developer')
    ->get();

Select Blade Directives

To keep the checkbox or select box in the checked/selected state you can use the blade directives.

<input type="checkbox"
        @checked(old('active', $user->active))
        name="active"/>


<select name="user_id">
    @foreach ($users as $row)
        <option value="{{ $row->id }}" @selected(old('id') == $row->id)>
            {{ $row->name }}
        </option>
    @endforeach
</select>

Implicit Route Binding

enum Category: string
{
    case Fruits = 'fruits';
    case People = 'people';
}

Route::get('/categories/{category}', function (Category $category) {
    return $category->value;
});

Bootstrap5- Pagination

Bootstrap 5 pagination view has been included in Laravel 9.

use Illuminate\Pagination\Paginator;

public function boot()
{
    Paginator::useBootstrapFive();
}

Functions and introduction to New Helpers

For str_contains, str_starts_with and str_ends_with PHP 8 string function is used internally.

str('Laravel')->append(' Article');
// 'Laravel Article'

str()->snake('LaravelArticle');
//laravel_article

//HTTP redirect to a name route
return to_route('posts.show', ['post' => 1]);

Introduction to a new Query Builder Interface in Laravel 9

Syntax

return Model::query()
    ->whereNotExists(function($query) {
        // $query is a Query\Builder
    })
    ->whereHas('relation', function($query) {
        // $query is an Eloquent\Builder
    })
    ->with('relation', function($query) {
        // $query is an Eloquent\Relation
    });

Server.php file

In Laravel 9 the server.php file that is used for php artisan serve will be removed from the root and will be included within the Laravel framework