Remove unnecessary $table, $primaryKey, and $timestamps properties from Eloquent models by adhering to standard Laravel conventions.
Declaring explicit overrides for standard default behaviors adds noise to Eloquent model definitions.
Laravel relies on convention over configuration: if your database table follows standard naming rules, zero configuration properties are required.
Over-Configured Model (Anti-Pattern)
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
// ❌ Redundant: Laravel already assumes table is 'articles'
protected $table = 'articles';
// ❌ Redundant: Laravel already assumes primary key is 'id'
protected $primaryKey = 'id';
// ❌ Redundant: Laravel already assumes auto-incrementing integer key
public $incrementing = true;
protected $keyType = 'int';
// ❌ Redundant: Laravel already expects timestamps by default
public $timestamps = true;
}
Clean Eloquent Model (Recommended)
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
protected $guarded = ['id'];
}
When to Explicitly Override
Declare properties only when deviating from standard conventions (such as legacy database tables or UUID primary keys):
class LegacyCustomer extends Model
{
protected $table = 'tbl_customers_legacy';
protected $primaryKey = 'customer_uuid';
public $incrementing = false;
protected $keyType = 'string';
}
Summary
- Eliminates boilerplate properties from model definitions.
- Keeps model files focused on relationships, scopes, and casts.
- Declare properties only when integrating with non-standard legacy database schemas.
Related Tips
View all tips →Encapsulate Complex Data Transformations with Custom Eloquent Casts
Implement the CastsAttributes interface to create reusable custom value object casts for Eloquent model attributes.
Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.