Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Last active August 11, 2022 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dkesberg/af725577d73c085835272d68a6152a9f to your computer and use it in GitHub Desktop.
Save dkesberg/af725577d73c085835272d68a6152a9f to your computer and use it in GitHub Desktop.
Eager loading specific columns in a nested relationship

Eager loading specific columns in a nested relationship

For eager loading a nested relationship you always have to include the primary key or the foreign key to the field list.

Wrong

$posts = Posts::with(['comments:id,title', 'comments.author:id,email']);

Right

$posts = Posts::with(['comments:id,author_id,title', 'comments.author:id,email']);

Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment