I am having an issue with Eloquent regarding removing child model: When this is executed in process2() I still have the deleted model which is not ok.
Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Model1 extends Model
{
public function seasons() {
return $this->hasMany('App\Models\Seasons', 'series_id', 'id');
}
}
Service
class Process {
public function process1($model1Instance) {
for($model1Instance->seasons() as $season) {
if(//whatever//) {
$season->delete();
}
}
}
public function process2($model1Instance) {
for($model1Instance->seasons() as $season) {
//At this point I still have the deleted instance
}
}
}
Usage
$proc = new Process();
......
$proc->process1($model1Instance);
$proc->process2($model1Instance);
When process1() removes a model from the parent, how can I have it removed in process2()?
Is it ok if I just unset the model and reassign the array of objects(without the removed one) to Model1 like:
public function process1($model1Instance) {
for($model1Instance->seasons() as $season) {
if(//whatever//) {
$season->delete();
} else {
$childs[]=$season;
}
}
$model1Instance->seasons = $childs
}
Tried:
$model1Instance->seasons()->detach($season);
but got: Call to undefined method Illuminate\Database\Query\Builder::detach()
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire