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\Collection,

What is HTTP client in laravel8 by laravenote 2021 | Laravelnote

Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing you to quickly make outgoing HTTP requests to communicate with other web applications. Laravel's wrapper around Guzzle is focused on its most common use cases and a wonderful developer experience. Before getting started, you should ensure that you have installed the Guzzle package as a dependency of your application. By default, Laravel automatically includes this dependency. However, if you have previously removed the package, you may install it again via Composer: composer require guzzlehttp/guzzle Making Requests To make requests, you may use the get, post, put, patch, and delete methods provided by the Http facade. First, let's examine how to make a basic GET request to another URL: use Illuminate\Support\Facades\Http; $response = Http::get('http://example.com'); The get method returns an instance of Illuminate\Http\Client\Response, which provides a variety of methods that may be use

Laravel Parallel Testing Is Now Available in laravel8 | Laravelnote

 Parallel Testing | Laravelnote As such we know Laravel and PHP Unit execute your tests sequentially within a single process.  As such laravel check the single process doesn’t use multiple cores so that therefore, your test execution is seriously bottlenecked! we glad to say that Parallel Testing is now available in Laravel. You can use this Laravel version8.25 you may also use to laravel8 built-in test Artisan command to run your cmd to tests simultaneously across multiple processes to use significantly reduce the time required for to run the entire test suite. It is about sure that in laravel8 new on top of Paratest Laravel automatically use to handles creating and migrating a test for database for each parallel process. In The  Laravel8 for testing purpose goodies - such as Storage::fake - are ready for used in Parallel too. Laravel Provide Each all individual laravel8 version use test suite will receive a varying benefits from parallel testing. In The Laravel Tests are execution wa