auditionadmin/app/Actions/Students/CreateStudent.php

40 lines
977 B
PHP

<?php
namespace App\Actions\Students;
use App\Exceptions\AuditionAdminException;
use App\Models\Student;
class CreateStudent
{
public function __construct()
{
}
public function __invoke(
string $firstName,
string $lastName,
int $grade,
array $optionalData = [],
int|string $school_id = 'user'
): Student {
if ($school_id === 'user') {
$school_id = auth()->user()->school_id;
}
if (Student::where('first_name', $firstName)->where('last_name', $lastName)
->where('school_id', $school_id)->exists()) {
throw new AuditionAdminException('Student already exists');
}
$newStudent = Student::create([
'first_name' => $firstName,
'last_name' => $lastName,
'grade' => $grade,
'school_id' => $school_id,
'optional_data' => $optionalData,
]);
return $newStudent;
}
}