staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems trying to update multiple records from a hasManyDeep relation

robman70 opened this issue · comments

Hello,
I'm using this library and the relations I've created are all working, great job!

Now I'm trying to do something that I can't do: I'm trying to modify (with the ->update()) method a set of records that I get from a hasManyDeep() relation but it doesn't work... it's possible to do it and I'm doing something wrong or is it not possible?

Example (within a Model):

$this->where('orders.deleted', 0)->update(['orders.deleted' => 1]); //FIRST case: works
$this->ordersRows()->where('orders_rows.deleted', 0)->update(['orders_rows.deleted' => 1]); //SECOND case: works
$this->ordersPackages()->where('orders_packages.deleted', 0)->update(['orders_packages.deleted' => 1]); //THIRD case: It does not work (no errors... it just doesn't do anything)

The FIRST case is a direct update on the object itself ($this) and it works without problems.

The SECOND case is an update on a set of records returned from a 'normal' relationship, i.e.:

public function ordersRows()
{
return $this->hasMany('App\Models\OrdersRow')->where('orders_rows.deleted', 0);
}

The THIRD case is an update on a set of records returned by a 'hasManyDeep' relationship (ordersPackages), i.e.:

public function ordersPackages()
{
return $this
  ->hasManyDeep(OrdersPackage::class, [OrdersRow::class, 'orders_rows_packages'])
  ->where('orders_packages.deleted', 0)
  ->where('orders_rows_packages.deleted', 0)
  ->where('orders_rows.deleted', 0)
  ->distinct(); // NOTE: I tried removing the distinct() but nothing changed
}

public function ordersRowsPackages()
{
return $this
  ->hasManyDeep(OrdersRowsPackage::class, [OrdersRow::class])
  ->where('orders_rows_packages.deleted', 0)
  ->where('orders_rows.deleted', 0)
  ->distinct(); // NOTE: I tried removing the distinct() but nothing changed
}

The relationships work perfectly, it's just the UPDATE that fails... I don't know if I did something wrong.

Hi @robman70,

//THIRD case: It does not work (no errors... it just doesn't do anything)

Please log the executed query.

Oh, damn!
I use the Query Log all the time to look for errors, this time (I don't know why), I didn't.
In fact, an SQL error did not occur but the query sequence was wrong and a constraint I had imposed on the relations prevented the update.
Thanks so much for the help!