Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zahra-ove/b3aef23d07bb1a154529f7e8d6632015 to your computer and use it in GitHub Desktop.
Save zahra-ove/b3aef23d07bb1a154529f7e8d6632015 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