Knowledgebase

Understanding Eloquent Models Print

  • phplaravel, permissions, guide, howto, solution, zillionkinghost, hosting, support
  • 0

The ORM.

WHAT A MODEL IS

A class representing a table, with instances representing rows.

WHAT IT PROVIDES

Reading, creating, updating and deleting, without writing queries Relationships between models Events at points in a record's life

WHAT TO DEFINE ON A MODEL

Which fields may be filled in bulk How fields should be cast to types Relationships

WHY THE BULK ASSIGNMENT SETTING MATTERS

Without it, submitted input could set any column.

That includes columns determining permissions.

WHAT TO NEVER DO

Permit every field to be filled from input Pass the whole request into a create or update

WHAT TO DO INSTEAD

Name explicitly the fields you accept.

WHAT CASTING DOES

Converts values to appropriate types automatically.

WHAT TO KEEP OUT OF MODELS

Business logic that does not belong to the record Query logic used in one place only

WHAT TO PUT ON A MODEL

Reusable query scopes Accessors for derived values Relationships

WHAT TO AVOID

Models that grow to hundreds of lines.


Was this answer helpful?
Back

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


Trustpilot