laravel-conventions
Laravel Conventions
These are non-negotiable personal conventions unless explicitly overridden by the user.
Models
No model observers, ever — Never use Laravel model observers or listen for Eloquent model events (creating, saved, etc.). Dispatch explicit domain events instead. This is non-negotiable. If they already exist for a given model, mention it to the user, but keep building.
If you are adding timestamps with millisecond precision, make sure that you write a $dateFormat on the model or in the cast. Otherwise, the precision is silently discarded on DB writes.
Always set the $table value. This avoids Laravel having to compute the table name every time it's needed.
Logging
Use Context@scope() liberally if important contextual data exists in a parent but not within the children, and the children are writing logs. This allows for maintaining contextual information inside of function calls without needing to pass that data to child functions.
Context::scope(function() use ($user) {
Context::add('user', $user->id);
$this->somePrivateMethod($user);
// ...
});
Helper Methods
Prefer new Collection() over collect(). This reduces an extra stack and indirection.
Using tap() is rarely the right call for readability.
Prefer CarbonImmutable::now() over now().
Dependency Injection
Prefer dependency injection to using facades. For large projects, test suite time is always a concern, and using facades disallow writing pure PHPUnit tests.
Testing
Use Model::factory()->make() whenever possible. Database writes are often unnecessary inside of tests.
More from cosmastech/skills
multi-model-code-review
>-
19multi-model-planning
>-
16php-conventions
Personal PHP conventions enforced when creating or modifying PHP files. Covers strict types, function imports, testing philosophy, class design, and observability. Activate whenever working on PHP code.
15planning-conventions
Personal conventions for planning software changes. Covers planning doc structure, failure scenario analysis, refactoring sequencing, observability planning, and multi-model review. Activate when planning features, writing design docs, or scoping technical work.
3