News

Factory makeMany() Method in Laravel 12.52.0

Published
Factory makeMany() Method in Laravel 12.52.0 image

Release updates

Never miss a Laravel release

Get an email whenever a new Laravel release lands.

Laravel v12.52.0 adds a makeMany() factory method, new withoutAfterMaking() and withoutAfterCreating() factory helpers, atomic writes in the Blade compiler to prevent race conditions, and improved exception traces for closures and standalone functions.

Key highlights include:

  • New makeMany() factory method
  • withoutAfterMaking() and withoutAfterCreating() factory helpers
  • temporaryUploadUrl() support for local filesystem
  • Atomic writes in BladeCompiler to prevent race conditions
  • Mailable::later() delay fix and delay assertions for queued mailables
  • LazyCollection::random() $preserveKeys support
  • String-based expressions for selectExpression()
  • Closures and standalone functions display correctly in exception traces
  • Numerous bug fixes and internal improvements

What's New

makeMany() Factory Method

Factories now include a makeMany() method as a complement to createMany(). Where createMany() persists records to the database, makeMany() returns a collection of model instances without saving them — useful when you need multiple in-memory instances for unit tests or transformations:

// Create multiple unsaved instances
$users = User::factory()->makeMany(3);
 
// Equivalent longhand
$users = User::factory()->count(3)->make();

Pull Request: #58795

withoutAfterMaking() and withoutAfterCreating() Factory Helpers

Two new factory helpers let you skip afterMaking and afterCreating callbacks on a per-call basis. This is useful when a callback sets up relationships or fires side effects that are irrelevant to the current test:

// Skip afterMaking callbacks
$user = User::factory()->withoutAfterMaking()->make();
 
// Skip afterCreating callbacks
$user = User::factory()->withoutAfterCreating()->create();

Pull Request: #58794

temporaryUploadUrl() for Local Filesystem

The temporaryUploadUrl() method now works with the local filesystem driver, previously only available for cloud drivers like S3. This makes it possible to use temporaryUploadUrl() in local development and testing environments without needing to swap to a cloud driver:

$url = Storage::disk('local')->temporaryUploadUrl(
'uploads/photo.jpg',
now()->addMinutes(30)
);

Pull Request: #58499

Atomic Writes in BladeCompiler

The Blade compiler now uses atomic writes when caching compiled views and inline component views. Previously, a race condition could occur when multiple processes compiled the same view simultaneously, resulting in corrupted or partially-written cache files. The atomic write approach writes to a temporary file first, then moves it into place, eliminating the race:

Pull Requests: #58812, #58815

Mailable::later() Delay Fix and Delay Assertions

Mailable::later() was not correctly applying the delay to the dispatched SendQueuedMailable job. This is now fixed. A companion PR adds delay support to Mail::assertQueued() so you can assert that a mailable was queued with a specific delay:

Mail::fake();
 
Mail::to('user@example.com')->later(now()->addMinutes(10), new WelcomeMail());
 
Mail::assertQueued(WelcomeMail::class, function ($mail) {
return $mail->delay->equalTo(now()->addMinutes(10));
});

Pull Requests: #58765, #58787

LazyCollection::random() $preserveKeys Support

LazyCollection::random() now accepts a $preserveKeys parameter, matching the behavior already available on Collection::random(). When true, the original keys are preserved in the returned results:

$collection = LazyCollection::make(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]);
 
// Preserves original keys
$sample = $collection->random(2, preserveKeys: true);
// e.g. ['b' => 2, 'd' => 4]

Pull Request: #58791

String-Based Expressions for selectExpression()

selectExpression() on the query builder now accepts raw strings in addition to Expression objects. This reduces boilerplate when you need a single raw expression in a select:

// Before
DB::table('orders')->selectExpression(DB::raw('SUM(total) as revenue'));
 
// After
DB::table('orders')->selectExpression('SUM(total) as revenue');

Pull Request: #58753

Closures Display Correctly in Exception Traces

Exception stack traces now correctly display closure and standalone function names instead of showing a generic or empty label. This makes debugging anonymous functions and Closure-based route handlers more readable in error pages and logs.

Pull Request: #58879

Bug Fixes and Improvements

Queue & Async:

  • Fix defer callbacks being discarded when using the sync queue (#58745)
  • Batch::progress() return value cast to int (#58767)

Database & Eloquent:

  • Fix empty Collection returned for non-model JSON:API resources (#58752)
  • Only merge cached casts for accessed attributes (#57627)
  • Revert SQL Server column precision checks that caused regressions (#58888)
  • MySQL connection string updated to use --ssl-mode=DISABLED for modern clients (#58786)

Testing:

  • Add option to opt out of parallel-safe cache prefix isolation (#58801)

Middleware:

  • Backport withMiddleware changes from 13.x (#58798)

Type Improvements & Documentation:

Upgrade Notes

No breaking changes are expected for typical applications. The SQL Server column precision revert restores behavior from before v12.51.0 — if you relied on the precision check behavior from that version, review your SQL Server migrations. Review the full changelog for complete details when upgrading.

References

Paul Redmond photo

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

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article