A test that asserts on the query count

An N+1 does not fail a test — the page renders correctly, just slowly — so the regression ships and a customer finds it three releases later.

public function testOrderIndexDoesNotNPlusOne(): void
{
    factory(Order::class, 20)->create();

    $count = 0;
    DB::listen(function () use (&$count) { $count++; });

    $this->get('/orders')->assertOk();

    $this->assertLessThan(10, $count, "query count regressed to {$count}");
}

An upper bound rather than an exact number is what makes it survivable: an exact assertion fails on every unrelated change and is deleted within a month. Twenty rows in the factory is the part that makes it meaningful, because an N+1 across three rows is four queries and passes any threshold. The failure message carrying the actual count saves the next person a debugging session.