Knowledgebase

Understanding Relationships Print

  • phplaravel, performance, troubleshooting, guide, howto, solution, zillionkinghost, hosting
  • 0

Connecting models.

WHAT KINDS EXIST

One record having one other One record having many A record belonging to another Many related to many, through an intermediate table Relationships through an intermediate model

WHAT TO DEFINE

Both directions, where both are used.

WHAT NAMING CONVENTIONS ASSUME

Predictable key names.

Where yours differ, state them explicitly.

WHAT RELATIONSHIPS ENABLE

Accessing related records naturally Querying based on related records Creating related records through the parent

WHAT TO BE CAREFUL WITH

Accessing a relationship inside a loop.

WHY

Each access issues a query.

WHAT THAT PRODUCES

One query for the list, then one per item.

THAT IS THE COMMONEST PERFORMANCE PROBLEM IN LARAVEL APPLICATIONS

WHAT TO DO ABOUT IT

Load the relationships in advance, with the initial query.

HOW TO SPOT IT

Query count rising with the number of records displayed.

WHAT TO USE

A development tool showing queries per request.


Was this answer helpful?
Back

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


Trustpilot