college-courses/app/Http/Livewire/Lesson/LessonEdit.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2024-10-15 10:15:40 +03:00
<?php
namespace App\Http\Livewire\Lesson;
use App\Models\Course;
use App\Models\Lesson;
use Closure;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Livewire\Component;
use Illuminate\Support\Str;
class LessonEdit extends Component implements HasForms
{
use InteractsWithForms;
public $name;
public $slug;
public $body;
public Course $course;
public $lesson;
public function mount($lesson)
{
$this->lesson = $this->course->getLesson($lesson);
$this->form->fill([
'name' => $this->lesson->name,
'slug' => $this->lesson->slug,
'body' => $this->lesson->body
]);
}
protected function getFormSchema(): array
{
return [
TextInput::make('name')
->label('Название занятия')
->required()
->reactive()
->afterStateUpdated(function (Closure $set, $state) {
$set('slug', Str::slug($state));
}),
TextInput::make('slug')
->label('Название в url-ссылке')
->required(),
MarkdownEditor::make('body')
];
}
public function update()
{
$this->lesson->updateOrFail($this->form->getState());
return redirect()->route('course-edit', ['course' => $this->course]);
}
public function render()
{
return view('livewire.lesson.lesson-edit')->layout('layouts.base');
}
}