Skip to main content

Create Controllers with a Custom Stub in Laravel 8.36

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
Revert "[8.x] Allow lazy collection to be instantiated from a generator" (#36844)
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)

Popular posts from this blog

Laravel8 in Serializes Models trait | laravelnote

This article was originally posted, with additional formatting, on my personal blog at laravel serializes model Background  When dispatching an object onto the queue, behind the scenes Laravel is recursively serializing the object and all of its properties into a string representation that is then written to the queue. There it awaits a queue worker to retrieve it from the queue and unserialize it back into a PHP object (Phew!). Problem When complicated objects are serialized, their string representations can be atrociously long, taking up unnecessary resources both on the queue and application servers. Solution Because of this, Laravel offers a trait called SerializesModels which, when added to an object, finds any properties of type Model or Eloquent\Collection during serialization and replaces them with a plain-old-PHP-object (POPO) known as a ModelIdentifier. These identifier objects represent the original properties Model type and ID, or IDs in the case of an Eloquent\Collecti...

Octane Support in Laravel forge 2021 5 may

 Back in March, Taylor announced the new Laravel Octane project at Laracon Online. Since then, we've been busy working on bug fixes and enhancements to Octane, as well as adding support to Laravel Forge. Today we're pleased to announce that Octane support is available in Forge. We've written the guide below to help you get started. Please keep in mind that Octane is still in beta and should not be used in production. Prerequisites Your project must require "laravel/octane": "^0.3.2" or above. Your server must have PHP 8.0 installed. You should then follow the Octane installation instructions listed in the Octane repository. Creating an Octane Site Octane can be enabled by selecting the Laravel Octane (Beta) project type option and PHP 8.0 as the PHP version that should be used to serve your site: Laravel Octane Project Type Laravel Octane Project Type Once the project type has been selected, Forge will ask for the port that Octane should listen on. Unlik...

Create Your Next Project's Readme in Laravel8 | laravelnote

  The readme.so editor gives you visual cues, starter section templates and includes many standard readme sections you're likely to use. It also has a nice preview to help guide you along the way. Never forget a section for your readme again! Select sections to add to your readme, edit the contents, and drag to rearrange. See a live-updating rendered preview of your markdown, then download your README.md file! Here's an example of starting an API section, which provides helpful formatting. You might need to specify API params: The editor includes both light and dark editor support, and you can download or copy/paste the raw markdown of your readme into version control once you're done. You can learn more about this project and start using it on  readme.so . Also, be sure to check out  readme.so on Product Hunt  and upvote it if you find it useful!