Knowledgebase

Working With Eloquent and the Database Layer Print

  • backenddevelopment, backend, database, migration, performance, guide, howto, solution
  • 0

Data access in Laravel.

WHAT THE ORM PROVIDES

Models mapping to tables, with relationships expressed in code.

WHAT RELATIONSHIPS EXIST

One to one, one to many, many to many, and polymorphic variants.

WHAT THE COMMONEST PERFORMANCE FAULT IS

Querying inside a loop, producing one query per record.

WHAT CAUSES IT

Accessing a relationship on each item of a collection.

WHAT SOLVES IT

Loading relationships in advance, in one query.

WHAT TO DO

Establish which relationships a view uses, and load them with the collection.

WHAT TO MONITOR

Query count per request, during development.

WHY

It reveals the fault immediately, and nothing else does.

WHAT MIGRATIONS PROVIDE

Schema changes as versioned code, applied consistently.

WHAT TO ALWAYS WRITE

A way to reverse each migration.

WHAT TO BE CAREFUL WITH

Migrations that cannot be reversed Migrations altering large tables without considering locking Mass assignment, without restricting which fields are fillable

WHY THAT LAST ONE

It allows a request to set fields it should not.

WHAT TO USE FOR MONEY

Exact decimal types, never floating point.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot