0

I am just about to ask, By Using this approach I can easily see there are three (3)

times call to server, So if I use in Model to Do that something like:

$this->subTown()->delete(); parent::someFunction(); // not the actual code

is there also three (3) calls to server behind the scene or Model will do the

same task only in One (1) Call.....!

because i wrote this code in controller rather than Model

I have used my approach to delete the two Relations or Hierarchy for this

situation like

City could have many Towns & Towns also could have many Sub-towns so when

Deleting the City than call this piece of code in controller's function

$id = Input::get('id');  //return response()->json($id);
$obj = SubTown::where('city_id',$id)->delete();
$objj = Town::where('city_id', $id)->delete();
$ok= City::find($id)->delete();

its working fine but i want to know the best approach.

can any one guide me in best way because According to mine its calling three times to server.

thanks in advance

Zeeshan DaDa
  • 197
  • 1
  • 10
  • 3
    You can use `cascade delete` in your database by setting up foreign keys. This depends on your dbms. – Harun Yilmaz Sep 16 '19 at 15:00
  • 4
    Possible duplicate of [Automatically deleting related rows in Laravel (Eloquent ORM)](https://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm) – Script47 Sep 16 '19 at 15:01
  • thanks for every one Can you tell me what will be the problem in future for me using this approach – Zeeshan DaDa Sep 16 '19 at 15:06
  • No particular problem, it's just that running three queries is not the most optimal. – George Hanson Sep 16 '19 at 15:17
  • 1
    If you setup relationships, you'd be able to do `$city->town->subtown->delete();`, `$city->town->delete();` and `$city->delete();` (sorry edit, with `hasMany()` relationships, you'd have to loop, but the idea still stands), and could easily write a method on your `City` model (replace `$city` with `$this`) that does this automatically when `$city` is called to be deleted. The suggested duplicate shows that logic, but does assume you have the relationships setup already. – Tim Lewis Sep 16 '19 at 15:24

0 Answers0