Factory makeMany() Method in Laravel 12.52.0

Last updated on by

Factory makeMany() Method in Laravel 12.52.0 image

Never Miss a Laravel Release 🚀

Sign up and get an email with each new Laravel release

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.

Cube

Laravel Newsletter

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

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Shift logo

Shift

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

Shift
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
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 $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

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

Tinkerwell

The latest

View all →
Route Metadata Support in Laravel 13.17 image

Route Metadata Support in Laravel 13.17

Read article
Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents image

Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents

Read article
Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower image

Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower

Read article
Showcase Your PhpStorm Expertise on LinkedIn image

Showcase Your PhpStorm Expertise on LinkedIn

Read article
Privacy Filter: Detect PII in Text from Laravel image

Privacy Filter: Detect PII in Text from Laravel

Read article
NationForge: A Self-Hosted Admin Panel for Civic Organizations image

NationForge: A Self-Hosted Admin Panel for Civic Organizations

Read article