Skip to content

[13.x] Allow array of pivot arrays to be passed to hasAttached#59723

Merged
taylorotwell merged 2 commits intolaravel:13.xfrom
jackbayliss:hasAttached-dx
Apr 16, 2026
Merged

[13.x] Allow array of pivot arrays to be passed to hasAttached#59723
taylorotwell merged 2 commits intolaravel:13.xfrom
jackbayliss:hasAttached-dx

Conversation

@jackbayliss
Copy link
Copy Markdown
Contributor

@jackbayliss jackbayliss commented Apr 16, 2026

Morning Taylor!

I am a real human who typed this with my hands, maybe a bit of copy & paste!
I'll be at Laravel Live UK in case someone wants to verify 😃

I see a lot of:

$user = User::factory()                                                                                                                                                                                                                                                                                                                                                                                                        
      ->hasAttached(Role::factory(), ['admin' => 'Y'])      
      ->hasAttached(Role::factory(), ['admin' => 'N']) // This becomes very long when you need a lot!
      .....
      ->create();                         

Or we reach for Sequence explicitly:

  $user = User::factory()  
      // Feels nasty to me 😞                                                                                                                                                                                                                                                                                                                                                                                                       
      ->hasAttached(Role::factory()->count(2), new Sequence(['admin' => 'Y'], ['admin' => 'N']))                                                                                                                                                                                                                                                                                                                              
      ->create();

This PR becomes the below, and is very useful when you need different pivot values:

  $user = User::factory()
      // It infers the count too  😎      
      ->hasAttached(Role::factory(), [['admin' => 'Y'], ['admin' => 'N']])                                                                                                                                                                                                                                                                                                                                 
      ->create();                             

No B/C afaik just a bit of added DX imo.

It uses sequence internally, so no big maintenance burden... and is the similar to the has{Relation} magic methods we did here : #59343

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review April 16, 2026 10:05
@taylorotwell taylorotwell merged commit 44c74eb into laravel:13.x Apr 16, 2026
51 of 52 checks passed
@jackbayliss jackbayliss deleted the hasAttached-dx branch April 16, 2026 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants