Everything is in the documentation
https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships
Location::findOrFail($locationId)->categories()->detach();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, I have 3 tables. locations (Location model) categories (Category model) location_category (hasn't model)
in the Location model, I have the "categories" function
public function categories()
{
return $this->belongsToMany(Category::class, 'location_category', 'location_id', 'category_id')
->withPivot('category_name', 'primary_category')
->withTimestamps();
}
in the Category model, I have the "locations" function
public function locations()
{
return $this->belongsToMany(Location::class, 'location_category', 'category_id', 'location_id')
->withPivot('category_name', 'primary_category')
->withTimestamps();
}
I want to delete location_category data where 'location_id' column = $locationId. How can I delete that if I know that the "location_category" table hasn't model?
@andrecuellar use withTrashed to include soft deleted results https://laravel.com/docs/8.x/eloquent#including-soft-deleted-models
Location::withTrashed()->findOrFail($locationId)->categories()->detach();
Please or to participate in this conversation.