This commit is contained in:
Saeed Vaziry
2024-09-27 20:36:03 +02:00
committed by GitHub
parent b62c40c97d
commit f6bc04763b
122 changed files with 6609 additions and 807 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace App\Actions\User;
use App\Models\Project;
use App\Models\User;
use Illuminate\Validation\Rule;
class UpdateProjects
{
public function update(User $user, array $input): void
{
$this->validate($input);
$user->projects()->sync($input['projects']);
if ($user->currentProject && !$user->projects->contains($user->currentProject)) {
$user->current_project_id = null;
$user->save();
}
/** @var Project $firstProject */
$firstProject = $user->projects->first();
if (!$user->currentProject && $firstProject) {
$user->current_project_id = $firstProject->id;
$user->save();
}
}
private function validate(array $input): void
{
validator($input, self::rules())->validate();
}
public static function rules(): array
{
return [
'projects.*' => [
'required',
Rule::exists('projects', 'id'),
],
];
}
}