57 lines
1.4 KiB
PHP
57 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\News;
|
|
use App\Models\Category;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Illuminate\Support\Str;
|
|
use Masmerise\Toaster\Toaster;
|
|
|
|
class NewsCreate extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
// Properti form berita
|
|
public $title, $image, $content;
|
|
public $category_id = '';
|
|
public $is_published = false;
|
|
|
|
protected $rules = [
|
|
'title' => 'required|min:5|unique:news,title',
|
|
'category_id' => 'required|exists:categories,id',
|
|
'image' => 'nullable|image|max:2048', // Max 2MB
|
|
'content' => 'required|min:20',
|
|
];
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
|
|
// Proses upload gambar
|
|
$imagePath = $this->image ? $this->image->store('news', 'public') : null;
|
|
|
|
News::create([
|
|
'title' => $this->title,
|
|
'slug' => Str::slug($this->title),
|
|
'category_id' => $this->category_id,
|
|
'image' => $imagePath,
|
|
'content' => $this->content,
|
|
'user_id' => auth()->id(),
|
|
'is_published' => $this->is_published,
|
|
]);
|
|
|
|
Toaster::success('Berita berhasil diterbitkan!');
|
|
|
|
return redirect()->route('admin.news');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.news.news-create', [
|
|
'categories' => Category::all()
|
|
]);
|
|
}
|
|
}
|