0

I have looked at several posts on the subject but I am confused.

I have 2 tables (activities & criteria) and a pivot table with an extra field (sortorder). My models are like this:

 // return all the activities associated with the criteria
 public function allActivities()
 {
      return $this->belongsToMany(Activity::class,'activity_criteria')->withPivot('sortorder');
 }

 // return all the criteria associated with this activity
 public function allCriteria()
 {
      return $this->belongsToMany(Criteria::class,'sc_activity_criteria')->withPivot('sortorder')->orderByPivot('sortorder');
 }

When I edit & save a criteria, I lose all the values of sortorder to NULL in the pivot table. How can I avoid that?

 // update record in db 
 $criteria->save();

 // detach old activities & attach the activities to populate the table activity_criteria
 $criteria->allActivities()->where('id',$criteria->id)->detach();
 $criteria->allActivities()->attach(request('activities'));

The problem is that when I edit a criteria, I do not know all the values of sortorder in all associated activities. Should I get that field in the edit form as well as an array ($sortorders)?

And then do something like this:

 $criteria->allActivities()->attach(request('activities'),['sortorder' => request('sortorders')]);
Paul Godard
  • 1,055
  • 1
  • 14
  • 30

0 Answers0