college-courses/app/Http/Livewire/Course/CourseEdit.php

99 lines
2.7 KiB
PHP

<?php
namespace App\Http\Livewire\Course;
use App\Models\Course;
use App\Models\Lesson;
use Closure;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Livewire\Component;
use Illuminate\Support\Str;
class CourseEdit extends Component implements HasForms, HasTable
{
use InteractsWithForms, InteractsWithTable;
public Course $course;
public $name;
public $slug;
public $author_id;
public function mount()
{
$this->form->fill([
'name' => $this->course->name,
'slug' => $this->course->slug,
]);
}
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-ссылке'),
];
}
protected function getFormModel(): Course
{
return $this->course;
}
public function save(): void
{
$this->course->update(
$this->form->getState(),
);
}
public function createLesson()
{
return redirect()->route('lesson-create', ['course' => $this->course]);
}
protected function getTableQuery()
{
return Lesson::query()->where('course_id', $this->course->id);
}
protected function getTableColumns(): array
{
return [
TextColumn::make('name'),
];
}
protected function getTableActions(): array
{
return [
Action::make('watch')->label('Посмотреть')->color('secondary')
->url(fn (Lesson $record): string => route('lesson', [
'course' => $this->course, 'lesson' => $record->slug
])),
Action::make('edit')->label('Редактировать')
->url(fn (Lesson $record): string => route('lesson-edit', [
'course' => $this->course, 'lesson' => $record->slug
])),
Action::make('delete')->label('Удалить')->color('danger')
->action(function (Lesson $record) {
return $record->delete();
}),
];
}
public function render()
{
return view('livewire.course.course-edit')->layout('layouts.base');
}
}