Custom Stub in Laravel 8.36

Sami Maxhuni
2 min readApr 10, 2021

--

The Laravel team released 8.36 with a custom stub option when creating controllers, a useCurrentOnUpdate method for blueprint datetime columns in MySQL, a dispatch_sync() helper function, and the latest changes in the 8.x branch:

Support useCurrentOnUpdate for MySQL Datetime Columns

Tim Martin contributed support for useCurrentOnUpdate for MySQL DATETIME column types:

This allows using DATETIME columns for metadata like created_at and updated_at more effectively. Since TIMESTAMP columns in MySQL suffer from the Year 2038 Problem , using DATETIME columns is preferred in some applications.

Here’s an example from the tests:

$blueprint = new Blueprint('users');$blueprint->dateTime('foo')->useCurrentOnUpdate();$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());$this->assertCount(1, $statements);$this->assertSame('alter table `users` add `foo` datetime on update CURRENT_TIMESTAMP not null', $statements[0]);

Queue dispatch_sync() Helper

Dries Vints contributed a dispatch_sync() helper function as an alternative to dispatch_now(). The dispatchSync() method is the recommended way to do synchronous dispatching (immediately). See the synchronous dispatching documentation for further details.

Allow Skipping TransformRequests Middleware via Closure

Taylor Otwell contributed the ability to skip TrimStrings and ConvertEmptyStringsToNull middleware. The pull request describes how this helps Laravel packages like Livewire and Octane:

Currently, Livewire has to do some nasty hacking to skip these middleware on certain requests and the approach is not suitable for use with Octane. Adding this feature will allow Livewire and other libraries to register a callback with this base middleware during the framework’s boot process to determine if the middleware should be skipped. That callback will receive the current request on each invocation.

Here are a few examples given in the pull request, but generally you’ll not need this functionality in Laravel apps unless you’re building very specific packages (i.e., Livewire) that need this advanced control:

TrimStrings::skipWhen(fn ($request) => shouldBeSkipped($request));ConvertEmptyStringsToNull::skipWhen(fn ($request) => shouldBeSkipped($request));

Custom Stub Type When Creating Controllers

Brian Dillingham contributed a --type flag for the make:controller command to define custom stub types for controllers:

php artisan make:controller CustomController --type=custom

Which would look for /stubs/controller.custom.stub in the application when creating the file. Custom stubs are helpful if you want more control and less manual editing when creating new controllers in a project.

Unfinished Option to the queue:prune-batches Command

Dries Vints contributed a --unfinished option to the queue:prune-batches console command that accepts an integer of hours to retain incomplete batch data. With the --unfinished option, the command will prune finished batches and unfinished batches based on the batch creation date older than X hours.

String Repeat Method

Jona Löffler contributed a repeat() method for the Str and Stringable classes. Here's an example from the pull request tests:

$this->assertSame('aaaaa', Str::repeat('a', 5));$this->assertSame('aaaaa', (string) $this->stringable('a')->repeat(5));

--

--

Sami Maxhuni

Full Stack Developer | PHP | Laravel | VueJS | ReactJS | MERN | MEVN | TALL Stacks