DEV Community

Morcos Gad
Morcos Gad

Posted on

Some tip for Laravel 8.76

Let's start quickly
I found some tip that I thought would be useful for you.
afterRefreshingDatabase method you can use in tests when you want to seed some data in a test directly after running migrations

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use LazilyRefreshDatabase;

    protected function afterRefreshingDatabase()
    {
        $this->artisan('db:seed', [
            '--class' => RoleAndPermissionSeeder::class
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

forbidden() and unauthorized method to the Response class. These methods clean up logic around these statuses nicely

// Before
if ($response->status() === 401) {
    // ...
}

if ($response->status() === 403) {
    // ...
}

// After
if ($response->unauthorized()) {
    // ...
}

if ($response->forbidden()) {
    // ...
}
Enter fullscreen mode Exit fullscreen mode

invisible modifier support, introduced in MySQL v8.0.23. When columns are marked as invisible, they are not implicitly (i.e., SELECT *) and thus not hydrated in Laravel models. These columns can still be explicitly selected, making it useful to omit unless you explicitly need the data
https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html

Schema::table('users', function (Blueprint $table) {
    $table->string('secret')->nullable()->invisible();
});
Enter fullscreen mode Exit fullscreen mode

finally :- substrReplace() to Str and Stringable classes

// Insert a string at a certain position
$string = '1300';

$result = Str::substrReplace($string, ':', 2, 0);
// '13:00'

// Replace the remainder of a string
$result = (string) Str::of('Laravel Framework')
    ->substrReplace('– The PHP Framework for Web Artisans', 8);
// 'Laravel – The PHP Framework for Web Artisans'
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code.
Source :- https://laravel-news.com/laravel-8-76-0

Oldest comments (0)