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

61 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2024-10-15 10:15:40 +03:00
<?php
namespace App\Http\Livewire\Course;
use App\Models\Course;
use Closure;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Illuminate\Support\Str;
use Filament\Forms\Components\Builder;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\Select;
class CourseCreate extends Component implements HasForms
{
use InteractsWithForms;
public $name;
public $slug;
public $author_id;
public function mount()
{
$this->author_id = Auth::id();
}
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(),
];
}
public function create()
{
$course = Course::create(
array_merge($this->form->getState(), ['user_id' => $this->author_id])
);
return redirect()->route('course-edit', ['course' => $course]);
}
public function render()
{
return view('livewire.course.course-create')->layout('layouts.base');
}
}