File: D:/HostingSpaces/SBogers10/firetech.komma.pro/app/KommaApp/Students/Kms/StudentService.php
<?php
namespace App\KommaApp\Students\Kms;
use App\KommaApp\Courses\Models\Course;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Students\Models\Student;
use App\KommaApp\Users\Models\Role;
use App\KommaApp\Users\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class StudentService extends SectionService
{
protected $sortable = false;
function __construct()
{
$this->forModelName = Student::class;
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, Collection $sectionTabItems): Model
{
/** @var Student $model */
//Process Student Specific attributes
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
$value = (int) $attribute->getValue();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromModel:
if($reference == 'course_id') {
$model->course_id = $value;
$attribute->mapValueFrom(Attribute::ValueFromItself, ''); //unset parent_id it so it won't get saved directly on the model in the parent_id field. The makeLastChildOf call above does save the parent id via lft rgt
}
break;
}
});
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the student
return $model;
}
}