Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

What’s New In Laravel 9 Features

Refer here
Refer here
whats-new-in-laravel-9
Refer here
Refers here
video

whats-new-in-laravel-9

What’s new in Laravel 9?

PHP 8 is the minimum requirement
Symfony Mailer replaced Swift Mailer
Controller route groups
Better accessors and mutators in Eloquent
Fulltext indexes and where clauses
The new Scout database engine
Breeze API with Next.js
Inline Blade rendering
New query builder interface

Image description

Better accessors and mutators in Eloquent

eloquent-mutators-and-accessors-example
eloquent-accessors-and-mutators-in-laravel-9/
laravel-9-eloquent-accessors-and-mutators-example

PHP 8 is the minimum requirement
Laravel uses Symfony 6, which requires at least PHP 8. PHP 8 comes with the new just-in-time (JIT) compiler, the OPcache extension, named arguments, and more.

Symfony Mailer replaced Swift Mailer
Swift Mailer, which has been used in Laravel for years, is being removed and will no longer be maintained. In Laravel v9 and future releases, you’ll have to use Symfony Mailer

Controller route groups
You can now use the controller method of the Laravel 9 Route class to define the controller that will be used for every route in a route group.

use App\Http\Controllers\PostController;

Route::controller(PostController::class)->group(function () {
    Route::get('/post/{id}', 'show');
    Route::post('/post', 'store');
});
Enter fullscreen mode Exit fullscreen mode

Better accessors and mutators in Eloquent
In Laravel 9, you can now use the Illuminate\Database\Eloquent\Casts\Attribute to declare a model prefix with a single non-prefixed term. Using one method call, you can now both get and set attributes.

use Illuminate\Database\Eloquent\Casts\Attribute;

public function username(): Attribute
{
  return new Attribute(
    get: fn ($value) => strtoupper($value),
    set: fn ($value) => $value,
  );
}

Enter fullscreen mode Exit fullscreen mode

In Laravel 9's Eloquent, accessors and mutators are used to modify the data retrieved from and saved to the database.

Accessors are used to modify the data retrieved from the database before it is returned to the application. They are defined as methods on the model and have a get prefix followed by the attribute name in StudlyCase.

Here's an example:

class User extends Model
{
    // ...

    public function getFullNameAttribute()
    {
        return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have defined a getFullNameAttribute accessor method that modifies the first_name and last_name attributes to create a full name. This accessor can be accessed like a regular attribute on the model:

$user = User::find(1);
echo $user->full_name;
Enter fullscreen mode Exit fullscreen mode

Mutators are used to modify the data before it is saved to the database. They are also defined as methods on the model and have a set prefix followed by the attribute name in StudlyCase.

Here's an example:

class User extends Model
{
    // ...

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have defined a setPasswordAttribute mutator method that hashes the value of the password attribute before it is saved to the database. This mutator will be called automatically whenever the password attribute is set on the model:

$user = new User;
$user->password = 'secret';
$user->save();
Enter fullscreen mode Exit fullscreen mode

In this example, the setPasswordAttribute mutator will be called automatically and the value of the password attribute will be hashed before it is saved to the database.

Fulltext indexes and where clauses
laravel-9-full-text
How-can-i-make-a-full-text-index-of-the-column

If you are using MySQL or PostgreSQL in your Laravel application, you can now use the fulltext method on the column definitions in your migration files to generate full-text indexes.

$table->text('content')->fullText();
Enter fullscreen mode Exit fullscreen mode

Then, you can use the whereFullText and orWhereFullText methods to add full-text where clauses to your queries.

$laravelPosts= DB::table('post')
           ->whereFullText('content', 'laravel')
           ->get();
Enter fullscreen mode Exit fullscreen mode

The new Scout database engine
Laravel v9 ships with the new Laravel Scout database engine. It provides full-text search capabilities to Eloquent models. It uses model observers to keep search indexes in sync with Eloquent records and is a good choice for applications that use a small- or medium-sized database or have a light workload. This engine will use “where-like” clauses when filtering results from your database.

To use it, just add the Laravel\Scout\Searchable trait to a model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Article extends Model
{
    use Searchable;
}
Enter fullscreen mode Exit fullscreen mode

Breeze API with Next.js
Laravel v9 includes a complimentary Next.js frontend implementation in its Breeze starter kit. By using this starter kit scaffolding, you can build Laravel applications that serve as both a backend and a JavaScript frontend using Laravel Sanctum authentication.

Laravel 10 brings improvements to the query builder

, making it more powerful and flexible. Here are some examples of how the improved query builder can be used:

Conditional clauses:
Conditional clauses can be used to build more complex queries. In Laravel 10, the query builder has a new when method that allows you to add a clause to the query conditionally based on a boolean value.

use Illuminate\Support\Facades\DB;

$query = DB::table('users')
            ->when($isAdmin, function ($query) {
                return $query->where('role', '=', 'admin');
            })
            ->get();
Enter fullscreen mode Exit fullscreen mode

In the above example, the when method checks if $isAdmin is true. If it is, it adds a where clause to the query to only return users with a role of 'admin'.

Join Subqueries:
Joining subqueries can be a powerful way to create more complex queries. In Laravel 10, the query builder has a new joinSub method that allows you to join a subquery as a table.

use Illuminate\Support\Facades\DB;

$subQuery = DB::table('orders')
                ->select('customer_id', DB::raw('SUM(amount) as total_sales'))
                ->groupBy('customer_id');

$query = DB::table('customers')
            ->joinSub($subQuery, 'sales', function ($join) {
                $join->on('customers.id', '=', 'sales.customer_id');
            })
            ->get();
Enter fullscreen mode Exit fullscreen mode

In the above example, we first create a subquery to get the total sales for each customer. We then join the subquery as a table and join it with the customers table. This allows us to query both tables at the same time and return the total sales for each customer.

Union Queries:
Union queries can be used to combine multiple queries into a single result set. In Laravel 10, the query builder has a new union method that allows you to union multiple queries together.

use Illuminate\Support\Facades\DB;

$query1 = DB::table('users')->where('active', '=', true);
$query2 = DB::table('users')->where('active', '=', false);

$query = $query1->union($query2)->get();
Enter fullscreen mode Exit fullscreen mode

In the above example, we create two queries to get active and inactive users separately. We then use the union method to combine the two queries into a single result set.

These are just a few examples of how the improved query builder in Laravel 10 can be used to create more powerful and flexible database queries.

Top comments (0)