<?php namespace Edulab\College\Actions; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Litepie\Actions\Concerns\AsAction; use Edulab\College\Models\Student; class StudentAction { use AsAction; protected $model; protected $namespace = 'edulab.college.student'; protected $eventClass = \Edulab\College\Events\StudentAction::class; protected $action; protected $function; protected $request; public function handle(string $action, Student $student, array $request = []) { $this->action = $action; $this->request = $request; $this->model = $student; $this->function = Str::camel($action); $this->executeAction(); return $this->model; } public function store(Student $student, array $request) { $attributes = $request; $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $student = $student->create($attributes); return $student; } public function update(Student $student, array $request) { $attributes = $request; $student->update($attributes); return $student; } public function destroy(Student $student, array $request) { $student->delete(); return $student; } public function copy(Student $student, array $request) { $count = $request['count'] ?: 1; if ($count == 1) { $student = $student->replicate(); $student->created_at = Carbon::now(); $student->save(); return $student; } for ($i = 1; $i <= $count; $i++) { $new = $student->replicate(); $new->created_at = Carbon::now(); $new->save(); } return $student; } }