tegos
tegos

Ivan Mykhavko

@tegos

Software engineer

14 Posts 279 Views

  • No matching results...
  • Searching...

/ 255

Poll Options

Ever struggled with factory relationships when two fields point to the same model? Turns out Laravel isn't guessing wrong, it's following its rules perfectly.

When you use `for($owner)` and `for($distributor)`, Laravel looks at model TYPE, not variable names. Both are Users, so it picks the first relationship and ignores the rest.

The fix? Be explicit: `->for($owner, 'user')` or just set foreign keys directly - I usually prefer that.

My take on @jclermont great insight. Sometimes being explicit beats magic.

dev.to/tegos/why-laravel-cant-guess-your-factory-relationships-4keb

62

Ever had a test pass locally but fail randomly in CI? That's what happened to me when testing timestamp fields.
The issue? Date::now() gets called twice - once in your code, once in your test - and those milliseconds add up.
The fix is simple: freeze time with Date::setTestNow() or $this->freezeTime() before making requests.
No more flaky tests, just reliable assertions.

dev.to/tegos/stop-flaky-tests-freeze-time-in-laravel-testing-1cnj


1

116

Yeah, nice.
When you use Date::now(), this can be simply configurable via provider, like Date::use(CarbonImmutable::class).

1

70

I recently saw advice to replace whereHas() with whereRelation() for "cleaner" code.
Yeah, whereRelation() is shorter, but honestly, I don't think that makes it better.

Here's the thing: whereRelation() looks like a simple column check, but it's actually filtering a relationship.
When you need multiple conditions (which happens often), you'll switch back to whereHas() anyway.

I stick with whereHas() everywhere.
It's clearer about what's happening and scales naturally when requirements change. Sometimes the slightly longer option is the better choice.
dev.to/tegos/wherehas-vs-whererelation-readability-over-shortcuts-1gk0


45

Ever wondered if PHP 8.5's pipe operator is actually worth the hype?
I was skeptical too. It looked weird at first, but I tested it in real Laravel projects and ran benchmarks.
Turns out, it's great for simple function chains like string cleanup, but gets messy with complex logic. Saw a 20-45% slowdown, though that's usually not a dealbreaker.
My take? Don't rewrite working code just for pipes. Use it if your team vibes with functional programming, otherwise stick with the classics.
Full breakdown with benchmarks in the article!
dev.to/tegos/php-85-pipe-operator-is-it-worth-using-4gig

53

Ever watched your staging server just die? Mine did. Both CPUs at 100%, memory maxed, totally frozen.

The culprit? Laravel's scheduler running heavy jobs from production. Price exports, syncs, data crunches just hammering a tiny 2 vCPU droplet.

The fix: split your schedule() by environment. Run heavy jobs once daily on staging instead of hourly. Keep only essentials.

Result? CPU dropped from 100% to 20%. Staging responsive, production untouched.

Don't copy production schedules blindly.

dev.to/tegos/dont-let-your-staging-server-die-separate-task-scheduling-in-laravel-420o

71