FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Eager Loading Without Eloquent: Laravel Collection hasMany
NewsWeb Development

Eager Loading Without Eloquent: Laravel Collection hasMany

via Dev.tovictorstackAI3h ago

import Tabs from ' @theme /Tabs'; import TabItem from ' @theme /TabItem'; I had two collections of plain arrays and needed to associate them relationally, but I was not working with Eloquent models. Laravel's Collection class is powerful, but it has no built-in way to express a one-to-many relationship between two arbitrary datasets. I built laravel-collection-has-many to fix that. What It Is laravel-collection-has-many is a small PHP library that registers a hasMany() macro on Laravel's Collection class. It lets you attach a related collection to a parent collection using foreign key and local key fields, exactly like Eloquent's eager loading, but for plain data. After calling $users->hasMany($posts, 'user_id', 'id', 'posts') , each user in the collection gains a posts property containing their matched items. flowchart LR A[Parent Collection\nusers] --> B[hasMany Macro] C[Related Collection\nposts] --> B B --> D[Group Related by Foreign Key\nO(n+m) single pass] D --> E[Attach to Paren

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles

News

The Shape of Inequalities

Lobsters • 29m ago

The Men Obsessed With ‘High T’
News

The Men Obsessed With ‘High T’

Wired • 34m ago

6 Darkest Truths About Programmers
News

6 Darkest Truths About Programmers

Medium Programming • 43m ago

Android Auto’s Secret Superpower Is a Customizable Shortcut Button
News

Android Auto’s Secret Superpower Is a Customizable Shortcut Button

Wired • 1h ago

News

The .NET GC Secrets That Only Runtime Engineers Talk About

Medium Programming • 1h ago

Discover More Articles