# Models
# No business logic
Avoid storing business logic inside models.
// Bad
class Post extends Model
{
public function publish()
{
$this->published = true;
$this->save();
}
}
Generally business logic should live in Actions.
// Good
class PublishPostAction
{
public function execute(Post $post)
{
$post->published = true;
$post->save();
}
}
# Magic methods
Avoid using magic methods when building queries.
// Bad
Post::wherePublished(true)->get();
// Good
Post::where('published', true)->get();
# Relations
All relations should use camelCase.
// Bad
public function published_posts()
{
//
}
// Good
public function publishedPosts()
{
//
}
# Scopes
It is fine to have few simple local scopes on model class directly. But if model uses complex scopes or big number of scopes, then extract scopes into custom query builder class.
All scopes should use camelCase.
# Accessors
Always use snake_case when using accessors.
This is to make them look and feel like native DB table columns.
// Bad
$user->fullName;
// Good
$user->full_name;