DEV Community

Morcos Gad
Morcos Gad

Posted on • Updated on

New Things Added - Laravel 9.17 and 9.18 Released

Let's get started quickly I found new things in Laravel 9.17 and 9.18 Released I wanted to share with you.

way to define nested eager loading relationships via a nested array

// Using dot notation
$books = Book::with('author.contacts')->get();

// Nested array
$books = Book::with([
    'author' => [
        'contacts',
        'publisher',
    ],
])->get();
Enter fullscreen mode Exit fullscreen mode

three convenience methods on the Illuminate Request instance to access underlying Symphony methods

$request->host(); // getHost()
$request->httpHost(); // getHttpHost()
$request->schemeAndHttpHost(); // getSchemeAndHttpHost()
Enter fullscreen mode Exit fullscreen mode

fake() helper function that allows you to easily access a singleton faker instance. Using this helper is helpful when prototyping, testing, and generating factory and seed data

@for($i = 0; $i < 10; $i++)
    <dl>
        <dt>Name</dt>
        <dd>{{ fake()->name() }}</dd>

        <dt>Phone</dt>
        <dd>{{ fake()->phoneNumber() }}</dd>

    </dl>
@endfor
Enter fullscreen mode Exit fullscreen mode

Here's an example of locale-specific faker usage

fake()->name() // config('app.faker_locale') ?? 'en_US'
fake('en_AU')->name() // en_AU
Enter fullscreen mode Exit fullscreen mode

Image description

// without Invokable

php artisan make:rule BirthYearRule
Enter fullscreen mode Exit fullscreen mode

let's add custom rules to our BirthYearRule

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class BirthYearRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return $value >= 1990 && $value <= date('Y');
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be between 1990 to '.date('Y').'.';
    }
}

// use 
use App\Rules\BirthYearRule;

 public function rules()
    {
        return [
            'name' => 'required',
            'birth_year' => [
                'required',
                new BirthYearRule()
            ]
        ];
    }
Enter fullscreen mode Exit fullscreen mode

// Now with Invokable

Image description

Image description

I hope you enjoyed with me and to learn more about this release visit the sources and search more. I adore you who search for everything new.
Source :- https://laravel-news.com/laravel-9-18-0
Source :- https://www.youtube.com/watch?v=DEMlr9HiuSU

Discussion (0)