/** * Sync a user with new organisations. * * @param \App\Models\User\User $user * @param \Illuminate\Support\Collection $organisations * * @return void */ public function sync(User $user, Collection $organisations) { $existing = Organisation::query()->whereIn('id', $organisations->lists('id'))->get(); $user->organisations()->detach(); $organisations = $this->map($organisations); $organisations = $this->create($organisations, $existing); $user->organisations()->attach($organisations->lists('id')->toArray()); }
/** * Get the organisation. * * @param \App\Models\User\User $user * @param string $organisation * * @return \App\Models\Organisation\Organisation */ protected function getOrganisation(User $user, $organisation) { $organisation = $user->organisations()->where('name', $organisation)->first(); if (!$organisation) { throw new NotFoundHttpException(); } return $organisation; }