forked from eatvio/eatvio-infra
same at 22.05.2023
This commit is contained in:
86
app/Http/Livewire/BlogPost/Create.php
Normal file
86
app/Http/Livewire/BlogPost/Create.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\BlogPost;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\WithFileUploads;
|
||||
use App\Models\Tag;
|
||||
use App\Models\BlogPost;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public $image = null;
|
||||
public $state = "archived";
|
||||
public $title = "";
|
||||
public $description = "";
|
||||
public $tags = [];
|
||||
public $videoUrl = "";
|
||||
|
||||
protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved'];
|
||||
|
||||
protected function rules()
|
||||
{
|
||||
$rules = [
|
||||
'image' => 'required|image|max:2048',
|
||||
'state' => 'required|in:active,archived',
|
||||
'description' => 'required|min:3',
|
||||
'videoUrl' => 'nullable|url|max:2048'
|
||||
];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.blog-post.create');
|
||||
}
|
||||
|
||||
public function parentComponentTagAdded(Tag $tag)
|
||||
{
|
||||
$tagIndex = $tag->id;
|
||||
$this->tags[$tagIndex] = $tag;
|
||||
}
|
||||
|
||||
public function parentComponentTagRemoved($tag)
|
||||
{
|
||||
$this->tags = collect($this->tags)
|
||||
->filter(fn($curTag) => $curTag['name'] != $tag)
|
||||
->all();
|
||||
}
|
||||
|
||||
public function draft()
|
||||
{
|
||||
$this->state = 'archived';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function preview()
|
||||
{
|
||||
$this->state = 'archived';
|
||||
$this->publish();
|
||||
}
|
||||
|
||||
public function publish()
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
$post = new BlogPost();
|
||||
$post->image = $this->image->store('blog_posts', 'public');
|
||||
$post->state = $this->state;
|
||||
$post->title = $this->title;
|
||||
$post->description = $this->description;
|
||||
$post->youtube_url = $this->videoUrl;
|
||||
$post->save();
|
||||
|
||||
$tagsIds = collect($this->tags)->map(fn($curTag) => $curTag['id']);
|
||||
$post->tags()->attach($tagsIds);
|
||||
|
||||
$user = Auth::user();
|
||||
$user->author_of_blog_post()->attach($post->id);
|
||||
|
||||
return redirect()->route('blog_post.show', $post->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user