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