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

jeroenvip's avatar

Records in user table not deleted belonging to a specific manager

Migrations

Models

This is working

  • When deleting admin the user table user is removed along with the admin record from the admin table.

  • When deleting agent the user table user is removed along with the admin record from the agent table.

  • When deleting a customer the user table user is removed along with the customer record from the customer table.

What is not working

  • When deleting a manager the user table users belonging to the manager are not removed. The records from the manager, agent and customer are remove. The manager itself are also removed.

(topic before was messed up)

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're facing is likely due to the way the relationships and cascading deletes are set up. When you delete a manager, you want to ensure that all related users are also deleted. However, it seems that the cascading delete is not properly configured for the User model when a Manager is deleted.

Here's a step-by-step solution to address this:

  1. Check the Relationships: Ensure that the Manager model has a proper relationship defined with the User model. It seems you already have a belongsTo relationship, which is correct.

  2. Cascading Deletes: Make sure that the cascadeOnDelete is properly set up in your migrations for the managers table. This ensures that when a manager is deleted, all related records in the users table are also deleted.

  3. Custom Deletion Logic: If cascading deletes are not sufficient, you might need to implement custom deletion logic in your Manager model to manually delete related users.

Here's how you can implement a custom deletion logic in the Manager model:

Explanation:

  • Boot Method: The boot method is used to hook into the model's lifecycle events. In this case, we're using the deleting event to perform actions before a manager is deleted.

  • Deleting Related Users: For each agent and customer related to the manager, we delete their associated user record. This ensures that all users related to the manager are removed.

  • Deleting Manager's User: If the manager itself has a user record that needs to be deleted, ensure that this is also handled.

By implementing this logic, you ensure that all users related to a manager are deleted when the manager is deleted. Make sure to test this thoroughly to ensure it behaves as expected in your application.

Please or to participate in this conversation.