Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

darep's avatar
Level 1

Eloquent\\Collection::attatch does not exist.

I'm very confused. When trying to use attatch I get this error.

I'm trying to do: return $user->interest->attatch($interestId);

I get the error in the title. I'm pretty sure my relations are right as I can access existing data in the pivot table with: $user->interest;

Any obvious thing I'm missing? I can't find anything on Google except something about permissions, but there's no info on where this code should be inserted.

0 likes
4 replies
MichalOravec's avatar

Any obvious thing I'm missing?

Parentheses.

 $user->interest()->attatch($interestId);
1 like
DevinPomsilina's avatar

Laravels higher order function calls, take a single method call, not multiple. Therefor if you create an helper method on the Item class, it will solve your problem.

class Item { public function attachCategories($categories) { $this->categories()->attach($categories); } } Which will make it possible to assign categories like so.

$items->each->attachCategories($request->categories);

Please or to participate in this conversation.