|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Livewire\Admin\Blog; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\View\Factory; |
|
8
|
|
|
use Illuminate\Contracts\View\View; |
|
9
|
|
|
use Illuminate\Foundation\Application; |
|
10
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
|
11
|
|
|
use Livewire\Attributes\On; |
|
12
|
|
|
use Livewire\Component; |
|
13
|
|
|
use Livewire\WithFileUploads; |
|
14
|
|
|
use Masmerise\Toaster\Toastable; |
|
15
|
|
|
use Xetaravel\Livewire\Forms\BlogArticleForm; |
|
16
|
|
|
use Xetaravel\Models\BlogArticle; |
|
17
|
|
|
use Throwable; |
|
18
|
|
|
|
|
19
|
|
|
class UpdateArticle extends Component |
|
20
|
|
|
{ |
|
21
|
|
|
use AuthorizesRequests; |
|
22
|
|
|
use Toastable; |
|
23
|
|
|
use WithFileUploads; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The form used to create/update a model. |
|
27
|
|
|
* |
|
28
|
|
|
* @var BlogArticleForm |
|
29
|
|
|
*/ |
|
30
|
|
|
public BlogArticleForm $form; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Used to show the update modal. |
|
34
|
|
|
* |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public bool $showModal = false; |
|
38
|
|
|
|
|
39
|
|
|
public function render(): Factory|Application|View|\Illuminate\View\View |
|
40
|
|
|
{ |
|
41
|
|
|
return view('livewire.admin.blog.update-article'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* When a user click on 'Edit' open the modal and set the content. |
|
46
|
|
|
* |
|
47
|
|
|
* @param BlogArticle $blogArticle |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
#[On('update-article')] |
|
52
|
|
|
public function updateArticle(BlogArticle $blogArticle): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->authorize('update', $blogArticle); |
|
55
|
|
|
|
|
56
|
|
|
$this->form->reset(); |
|
57
|
|
|
$this->form->fill([ |
|
58
|
|
|
'blogArticle' => $blogArticle, |
|
59
|
|
|
'title' => $blogArticle->title, |
|
60
|
|
|
'blog_category_id' => $blogArticle->blog_category_id, |
|
61
|
|
|
'content' => $blogArticle->content, |
|
62
|
|
|
'published_at' => $blogArticle->published_at?->format('Y-m-d H:i'), |
|
63
|
|
|
]); |
|
64
|
|
|
$this->form->searchCategories(); |
|
65
|
|
|
|
|
66
|
|
|
$this->showModal = true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Update the article. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
* |
|
74
|
|
|
* @throws Throwable |
|
75
|
|
|
*/ |
|
76
|
|
|
public function update(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->authorize('update', $this->form->blogArticle); |
|
79
|
|
|
|
|
80
|
|
|
$this->validate(); |
|
81
|
|
|
|
|
82
|
|
|
$this->form->update(); |
|
83
|
|
|
|
|
84
|
|
|
redirect() |
|
85
|
|
|
->route('admin.blog.article.index') |
|
|
|
|
|
|
86
|
|
|
->success('Your article has been updated successfully !'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|