Updating the user when attaching a pivot element

Posted 2 years ago by stueynet

Currently when you attach a pivot element to an object, it creates the relationship, but does not change the actual object. For example:

$user = auth()->user();
$user->books()->attach($book_id);
$user->books->count() // returns 0

When you reload a page or get the user again, it has it. For example:

$user = auth()->user();
$user->books()->attach($book_id);
$user->books->count() // returns 0
auth()->user()->books->count() // returns 1

Please sign in or create an account to participate in this conversation.