ojessecruz
ojessecruz

Jessé Cruz

@ojessecruz

Brazilian dev 🇧🇷🇺🇸 | Building http://elenya.app | Dad of 2 | Powered by coffee ☕
71 Posts 3K Views
  • No matching results...
  • Searching...

/ 255

Poll Options

🔍 #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!

82

🚨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