Issues (264)

app/Livewire/Discuss/DeleteConversation.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Discuss;
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 Illuminate\Http\RedirectResponse;
12
use Illuminate\Support\Facades\DB;
13
use Livewire\Attributes\On;
14
use Livewire\Component;
15
use Livewire\Features\SupportRedirects\Redirector;
16
use Masmerise\Toaster\Toastable;
17
use Xetaravel\Models\DiscussConversation;
18
use Throwable;
19
20
class DeleteConversation extends Component
21
{
22
    use AuthorizesRequests;
23
    use Toastable;
24
25
    /**
26
     * The form used to create/update a model.
27
     *
28
     * @var DiscussConversation
29
     */
30
    public DiscussConversation $discussConversation;
31
32
    /**
33
     * Used to show the Edit/Create modal.
34
     *
35
     * @var bool
36
     */
37
    public bool $showModal = false;
38
39
    public function mount(DiscussConversation $discussConversation): void
40
    {
41
        $this->discussConversation = $discussConversation;
42
    }
43
44
    public function render(): Factory|Application|View|\Illuminate\View\View
45
    {
46
        return view('livewire.discuss.delete-conversation');
47
    }
48
49
    /**
50
     * Show the confirmation modal to delete a conversation.
51
     *
52
     * @return void
53
     */
54
    #[On('delete-conversation')]
55
    public function deleteConversation(): void
56
    {
57
        $this->authorize('delete', $this->discussConversation);
58
59
        $this->showModal = true;
60
    }
61
62
    /**
63
     * Delete a conversation.
64
     *
65
     * @return RedirectResponse|Redirector|null
66
     *
67
     * @throws Throwable
68
     */
69
    public function delete(): Redirector|RedirectResponse|null
70
    {
71
        $this->authorize('delete', $this->discussConversation);
72
73
        // We need to re-fetch the conversation for loading relations to prevent lazy loads.
74
        $this->discussConversation = DiscussConversation::with('category', 'posts', 'posts.conversation', 'discussLogs')
75
            ->findOrFail($this->discussConversation->id);
76
77
        $result = DB::transaction(function () {
78
            return $this->discussConversation->delete();
79
        });
80
81
        if ($result) {
82
            return redirect()
83
                ->route('discuss.index')
0 ignored issues
show
The method route() does not exist on Illuminate\Routing\Redirector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
                ->/** @scrutinizer ignore-call */ route('discuss.index')

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.

Loading history...
84
                ->success('This discussion has been deleted successfully !');
85
        }
86
87
        $this->showModal = false;
88
89
        return back()
90
            ->error('An error occurred while deleting this discussion !');
91
    }
92
}
93