1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xetaravel\Livewire\Discuss; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
6
|
|
|
use Illuminate\Http\RedirectResponse; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Livewire\Attributes\On; |
9
|
|
|
use Livewire\Component; |
10
|
|
|
use Livewire\Features\SupportRedirects\Redirector; |
11
|
|
|
use Masmerise\Toaster\Toastable; |
12
|
|
|
use Throwable; |
13
|
|
|
use Xetaravel\Events\Discuss\PostWasDeletedEvent; |
14
|
|
|
use Xetaravel\Models\DiscussPost; |
15
|
|
|
|
16
|
|
|
class DeletePost extends Component |
17
|
|
|
{ |
18
|
|
|
use AuthorizesRequests; |
19
|
|
|
use Toastable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The form used to create/update a model. |
23
|
|
|
* |
24
|
|
|
* @var DiscussPost |
25
|
|
|
*/ |
26
|
|
|
public DiscussPost $discussPost; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Used to show the Edit/Create modal. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
public bool $showModal = false; |
34
|
|
|
|
35
|
|
|
public function render() |
36
|
|
|
{ |
37
|
|
|
return view('livewire.discuss.delete-post'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Show the confirmation modal to delete a post. |
42
|
|
|
* |
43
|
|
|
* @param DiscussPost $discussPost |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
#[On('delete-post')] |
48
|
|
|
public function deletePost(DiscussPost $discussPost): void |
49
|
|
|
{ |
50
|
|
|
$this->authorize('delete', $discussPost); |
51
|
|
|
|
52
|
|
|
$this->discussPost = $discussPost; |
53
|
|
|
|
54
|
|
|
$this->showModal = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Delete a conversation. |
59
|
|
|
* |
60
|
|
|
* @return RedirectResponse|Redirector|null |
61
|
|
|
* |
62
|
|
|
* @throws Throwable |
63
|
|
|
*/ |
64
|
|
|
public function delete(): RedirectResponse|Redirector|null |
65
|
|
|
{ |
66
|
|
|
$this->authorize('delete', $this->discussPost); |
67
|
|
|
|
68
|
|
|
if ($this->discussPost->conversation->first_post_id === $this->discussPost->getKey()) { |
69
|
|
|
$this->showModal = false; |
70
|
|
|
$this->error('You can not delete the first post of a discussion !'); |
71
|
|
|
|
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$result = DB::transaction(function () { |
76
|
|
|
return $this->discussPost->delete(); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
if ($result) { |
80
|
|
|
event(new PostWasDeletedEvent($this->discussPost->conversation, $this->discussPost->user)); |
81
|
|
|
|
82
|
|
|
return redirect() |
83
|
|
|
->route( |
|
|
|
|
84
|
|
|
'discuss.conversation.show', |
85
|
|
|
['id' => $this->discussPost->conversation->getKey(), 'slug' => $this->discussPost->conversation->slug] |
86
|
|
|
) |
87
|
|
|
->success('This post has been deleted successfully !'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return redirect() |
91
|
|
|
->route('discuss.post.show', ['id' => $this->discussPost->getKey()]) |
92
|
|
|
->error('An error occurred while deleting this post !'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.