Create Controllers with a Custom Stub in Laravel 8.36

Published on by

Create Controllers with a Custom Stub in Laravel 8.36 image

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));

Release Notes

You can see the full list of new features and updates below and the diff between 8.35.0 and 8.36.0 on GitHub. The following release notes are directly from the changelog:

v8.36.0

Revert

Added

  • Added support useCurrentOnUpdate for MySQL datetime column types (#36817)
  • Added dispatch_sync() helper (#36835)
  • Allowing skipping TransformRequests middlewares via Closure (#36856)
  • Added type option to make controller command (#36853)
  • Added missing return $this to Illuminate\Support\Manager::forgetDrivers() (#36859)
  • Added unfinished option to PruneBatchesCommand (#36877)
  • Added a simple Str::repeat() helper function (#36887)

Fixed

  • Fixed getMultiple and increment / decrement on tagged cache (0d21194)
  • Implement proper return types in cache increment and decrement (#36836)
  • Fixed blade compiler regex issue (#36843, #36848)
  • Added missing temporary_url when creating flysystem (#36860)
  • Fixed PostgreSQL schema:dump when read/write hosts are arrays (#36881)

Changed

  • Improve the exception thrown when JSON encoding response contents fails in Response::setContent() (#36851, #36868)
  • Revert isDownForMaintenance function to use file_exists() (#36889)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

Visit DocuWriter.ai
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Generate Code Coverage in Laravel With PCOV image

Generate Code Coverage in Laravel With PCOV

Read article
Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1 image

Non-backed Enums in Database Queries and a withSchedule() bootstrap method in Laravel 11.1

Read article
Laravel Pint --bail Flag image

Laravel Pint --bail Flag

Read article
Laravel Herd for Windows is now released! image

Laravel Herd for Windows is now released!

Read article
The Laravel Worldwide Meetup is Today image

The Laravel Worldwide Meetup is Today

Read article
Cache Routes with Cloudflare in Laravel image

Cache Routes with Cloudflare in Laravel

Read article