0

I'm new to Laravel and trying to soft-delete both rows from two tables.

Vehicles table,

  • id
  • license
  • brand
  • is_taken
  • Taken_bies table,

  • id
  • name
  • phone
  • vehicle_id
  • In vehicles table, if is_taken is true, I grab the id of vehicle and fill it to vehicle_id from taken_bies table with other information.

    In my TakenBy model, I've implemented relationship as follow:

    public function vehicle() {
    return $this->belongsTo('App\Vehicle');
    }
    

    My Requiremnet:
    If I soft-delete the vehicle, I want to delete the related taken_bies information from taken_bies table. How can I achieve that? I'm using Laravel 5.8. Thank you.

    Siri
    • 79
    • 1
    • 15
    • 1
      Check this answer it might help you with your problem. [Link](https://stackoverflow.com/questions/58498901/getdirty-soft-deleted-item-in-deleted-observer-method-laravel-5-8/58500901#58500901) You can override your delete function and add code to delete take_bies row whenever vehicle table row deleted. – Vishal Tarkar Nov 20 '19 at 05:20
    • 1
      Does this answer your question? [Automatically deleting related rows in Laravel (Eloquent ORM)](https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm) – zahid hasan emon Nov 20 '19 at 05:28

    1 Answers1

    0

    you can delete it like below

    $vehicle = Vehicle::find(1);
    $vehicle->taken_bies()->delete();
    $vehicle->delete();
    
    Tanvir Ahmed
    • 969
    • 12
    • 24