File: D:/HostingSpaces/SBogers10/ehboledensysteem.komma.pro/app/Console/Commands/Archiver.php
<?php
namespace App\Console\Commands;
use App\KommaApp\ArchivedCourses\Models\ArchivedCourse;
use App\KommaApp\ArchivedCourses\Models\ArchivedCourseTranslation;
use App\KommaApp\ArchivedEvents\Models\ArchivedEvent;
use App\KommaApp\ArchivedEvents\Models\ArchivedEventTranslation;
use App\KommaApp\Courses\Models\Course;
use App\KommaApp\Courses\Models\CourseTranslation;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\Events\Models\EventTranslation;
use Illuminate\Console\Command;
use Carbon\Carbon;
class Archiver extends Command
{
protected $commandsService;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'archiver:run';
/**
* IndexCatalog constructor.
*/
public function __construct( )
{
parent::__construct();
}
/**
* Execute the command.
*
* @return void
*/
public function handle()
{
$this->info('Done.'.PHP_EOL);
// all courses are not yet archived
$courses = Course::all();
foreach ($courses as $course) {
$carbonDate = Carbon::parse($course->date)->addDay();
//check which project must created
if (Carbon::now()->greaterThan($carbonDate)) {
$archivedCourse = new ArchivedCourse();
$archivedCourse->fill($course->getAttributes());
$archivedCourse->save();
$course->translations()->get()->each(function(CourseTranslation $courseTranslation) use($archivedCourse, $course) {
$archivedCourseTranslation = new ArchivedCourseTranslation();
$archivedCourseTranslation->fill($courseTranslation->getAttributes());
$archivedCourseTranslation->translatable()->associate($archivedCourse);
// $courseTranslation->translatable()->dissociate($course);
// $courseTranslation->delete();
$archivedCourseTranslation->save();
});
$archivedCourse->competences()->saveMany($course->competences()->get());
$archivedCourse->users()->saveMany($course->users()->get());
$course->competences()->detach();
$course->users()->detach();
$course->delete();
}
}
$events = Event::all();
foreach ($events as $event) {
$carbonDate = Carbon::parse($event->date)->addDay();
//check which project must created
if (Carbon::now()->greaterThan($carbonDate)) {
$archivedEvent = new ArchivedEvent();
$archivedEvent->fill($event->getAttributes());
$archivedEvent->save();
$event->translations()->get()->each(function(EventTranslation $eventTranslation) use($archivedEvent, $event) {
$archivedEventTranslation = new ArchivedEventTranslation();
$archivedEventTranslation->fill($eventTranslation->getAttributes());
$archivedEventTranslation->translatable()->associate($archivedEvent);
// $eventTranslation->translatable()->dissociate($event);
// $eventTranslation->delete();
$archivedEventTranslation->save();
});
$archivedEvent->users()->saveMany($event->users()->get());
// $event->translations()->dissociate();
$event->users()->detach();
$event->delete();
}
}
}
}