> For the complete documentation index, see [llms.txt](https://lauradang.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lauradang.gitbook.io/notes/web/laravel/database-queries.md).

# Database Queries

## Deleting rows with foreign key constraints

1. Figure out all the relationships of the table of the row you are trying to delete (go to model\_name.php and check for belongsToMany, HasMany, etc.)
2. Create a delete helper function in model\_name.php and use `detach()` and `delete()` as necessary

   ```php
   public function delete()
   {
    $this->grants()->detach();
    $this->fippaImports()->detach();
    $this->documents()->delete();

    parent::delete();
   }
   ```
3. Call this function in the controller

   ```php
   public function destroy(FippaTracking $fippaTracking)
   {
    $fippaTracking->delete();

    return redirect()
        ->route('fippa_trackers')
        ->withSuccess('Fippa Tracker successfully deleted.');
   }
   ```
