mercredi 22 mai 2019

Updating pivot table and keeping old values

I am working in Laravel 5.8 for a project, and I need to do an update on a pivot table and keeping the value I don't update.

Here's my class Calendar :

class Calendar extends Model {

public function users() {
    return $this->belongsToMany('App\Models\User')->withPivot('display', 'all_rights', 'color', 'rgbCode');
    }
}

Here's my class User

class User extends Model {
public function calendars() {
    return $this->belongsToMany('App\Models\Calendar')->withPivot('display', 'all_rights', 'color', 'rgbCode');
    }
}

All users can have various calendars in which they'll have events. When I create a calendar, I assign the users in to different lists to make them admin or simple users :

public function create(Request $request)
{
    $calendar = new Calendar();
    $calendar->name = $request->input('name');
    $calendar->save();
    $calendar->users()->attach($request->input('users'), ['color' => $color->name, 'rgbCode' => $color->rgbCode]);
    $calendar->users()->attach($request->input('administrators'), ['all_rights' => 1,'color' => $color->name, 'rgbCode' => $color->rgbCode]);
}

At the creation, all Calendars are set with display : null. An user can later toggle the display.

The problem is that when I want to update a calendar (name or people allowed), the sync doesn't really work as I would like :

public function update(Request $request)
{
    $calendar = Calendar::find($request->input('id'));
    $calendar->name = $request->input('name');
    $calendar->update();

    $calendar->users()->sync($request->input('users'));
    $calendar->users()->sync($request->input('administrators'), ['all_rights' => 1]);
}

If I don't precise values in the array in the sync, it'll go as the default value, so null. It's okay for the all_rights value in $calendar->users()->sync($request->input('users')); because I need it to be at null, but for the display it is problematic, as the user could be annoyed that the fact that his colleague changed one setting in the calendar, he has to update it to see it again.

So my question : is it possible to update in mass with sync(), and keeping the old values if they exists ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire