ojessecruz

Jessé Cruz

Verified

@ojessecruz

👨🏻‍💻 Software Engineer 🇧🇷🇺🇸 | Laravel Artisan | Always learning

72 Posts 3K Views

0 / 255

🔍 #LaravelTip: Need a job to execute immediately within another job?

❌ Job::dispatch()->onConnection('sync')

✅ Job::dispatchSync()

The first one just sets queue connection, while dispatchSync() guarantees immediate execution before next line.

Key for data consistency!

79

🚨Laravel Tip: Avoid Race Conditions in Jobs

❌Bad:
$model->items()->create([...]);
$item = $model->items()->first();

✅Good:
$item = $model->items()->create([...]);

Why? Even in isolated jobs, first() can return null because:
- New DB query
- Cache inconsistency
- Commit delays

Always store the create() result directly!

71