I've created two classes extending Eloquent (contacts and tags), they have a ManyToMany relationship. I'm trying to create the method for un-tagging a contact, but am unable to find any documentation to tell how to remove the entry in the relation-table without deleting either the tag itself or the contact.
So far, I've tried
$contact = Contact::find($contact_id);
$tag = $contact->tags->where('id', '=', $id);
$tag->delete();
This only deletes the contact. It makes sense that it doesn't work, but I'm not sure what else to try. I don't want to delete the contact or the tag, just the relationship between the two.
I've also now tried:
$tag = Tag::find($id);
$tag->contacts->detach($contact_id);
This gives me the error:
BadMethodCallException in Builder.php line 2071: Call to undefined method Illuminate\Database\Query\Builder::detach()
as well as
$tag = Tag::find($id);
$contact = $tag->contacts->find($contact_id);
$tag->contacts->detach($contact);
This gives me the error:
FatalErrorException in Tag.php line 34: Call to undefined method Illuminate\Database\Eloquent\Collection::detach()
Both the Contacts and Tags classes extend Illuminate\Database\Eloquent\Model;
To delete a model directly, call delete() on it and don't define a deleting listener in its boot method or define an empty deleting method. If you want to further delete relations of a related model, you will define a deleting listener in the boot method of that model and delete the relations there.
This Laravel/Lumen package provides application level cascading deletes for the Laravel's Eloquent ORM. When referential integrity is not, or cannot be, enforced at the data storage level, this package makes it easy to set this up at the application level.
In Eloquent, you can delete database records conveniently with the delete method from the parent Model class.
You can use detach
for many-to-many relationships
http://laravel.com/docs/5.1/eloquent-relationships#inserting-many-to-many-relationships
You just pass in the ID of the Tag. Take note of the parentheses after "tags"
$contact->tags()->detach($id);
Since it's many-to-many you could do the reverse as well
$tag->contacts()->detach($contact_id);
Similarly, you can use attach
to create relationships. Just guessing since you didn't know about detach that you probably could use attach as well. Attach can take a single ID or an array of IDs (plus some more advanced options)
$contact->tags()->attach($id);
$contact->tags()->attach([$id1, $id2, ...]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With