在 Livewire 模态中删除项目会显示 404 未找到弹出窗口

问题描述 投票:0回答:1

我正在使用此存储库中的 livewire 模态:https://github.com/wire-elements/modal

因此,我创建了一个编辑模式,它也可以删除该项目。这是模态的样子

enter image description here

有删除按钮。当我点击它时,它实际上被删除了。没有给出错误,模式关闭,但一秒钟后,另一个弹出窗口显示 404 not find

enter image description here

即使我无法添加其他帐户,当我单击按钮添加其他帐户时,它仍然显示 404 未找到。解决此问题的唯一方法是刷新页面即可正常工作。

这是AccountMediumEditModal.php中的删除功能

public function delete()
{
    // Check if account medium still exists
    if (!$this->accountMedium->exists) {
        $this->dispatch('show-toast', ['status' => 'error', 'message' => 'The item has already been deleted.']);
        return;
    }
    $controllerAccountMedium = new AccountMediumController();
    $res = $controllerAccountMedium->destroy(accountMedium: $this->accountMedium);

    if ($res->status() != 200) {
        $this->dispatch('show-toast', ['status' => 'error', 'message' => $res->getData()->message]);
        return;
    }

    $this->dispatch('show-toast', ['status' => 'success', 'message' => $res->getData()->message]);
    $this->closeModal();
    $this->destroyOnClose();
}

如果您想在完整模式下查看它,这里是完整的代码

class AccountMediumEditModal extends ModalComponent
{
    public AccountMedium $accountMedium;
    public $projectCategories;
    public $mediumAccount;

    // Model
    public $created_by;
    public $account_medium_id;
    public $allowed_categories = [];
    public $recommended_password;
    public $name;
    public $username;
    public $password;
    public $blog_url;
    public $token;
    public $token_expired_at;

    public function mount()
    {

        if ($this->accountMedium) {
            $this->created_by = $this->accountMedium->created_by;
            $this->recommended_password = $this->accountMedium->recommended_password;
            $this->name = $this->accountMedium->name;
            $this->allowed_categories = $this->accountMedium->allowed_categories;
            $this->username = $this->accountMedium->username;
            $this->password = $this->accountMedium->password;
            $this->blog_url = $this->accountMedium->blog_url;
            $this->token = $this->accountMedium->token;
            $this->token_expired_at = $this->accountMedium->token_expired_at;
        }
    }
    public function render()
    {
        // Define token expiration +2 month by default
        if (!$this->token_expired_at) {
            $currentTime = Carbon::now();
            $this->token_expired_at = $currentTime->modify('+2 month')->format('Y-m-d\TH:i');
        }
        // Define recommended password to use
        $this->recommended_password = $this->accountMedium->account->recommended_password;
        // Get project category
        $this->projectCategories = ProjectCategory::all();
        return view('livewire.modal.account.account-medium-edit-modal');
    }

    protected $rules = [];
    public function edit()
    {
        $this->account_medium_id = $this->accountMedium->id;
        $this->created_by = Auth::user()->id;

        // Replacing http with https
        $this->blog_url = str_replace('http://', 'https://', $this->blog_url);
        // Fix when blog_url has no https
        if (!Str::contains($this->blog_url, 'https://')) {
            $this->blog_url = 'https://' . $this->blog_url;
        }
        $accountMediumEditRequest = new AccountMediumEditRequest();
        $this->rules = $accountMediumEditRequest->rules();
        $input = $this->validate();
        $accountMediumEditRequest->merge($input);

        $controllerAccountMedium = new AccountMediumController();
        $res = $controllerAccountMedium->update(accountMediumEditRequest: $accountMediumEditRequest);

        if ($res->status() != 200) {
            $this->dispatch('show-toast', ['status' => 'error', 'message' => $res->getData()->message]);
            return;
        }

        $this->dispatch('show-toast', ['status' => 'success', 'message' => $res->getData()->message]);
        $this->dispatch('refresh');
        $this->closeModal();
    }

    public function delete()
    {
        // Check if account medium still exists
        if (!$this->accountMedium->exists) {
            $this->dispatch('show-toast', ['status' => 'error', 'message' => 'The item has already been deleted.']);
            return;
        }
        $controllerAccountMedium = new AccountMediumController();
        $res = $controllerAccountMedium->destroy(accountMedium: $this->accountMedium);

        if ($res->status() != 200) {
            $this->dispatch('show-toast', ['status' => 'error', 'message' => $res->getData()->message]);
            return;
        }

        $this->dispatch('show-toast', ['status' => 'success', 'message' => $res->getData()->message]);
        $this->dispatch('refresh');
        $this->closeModal();
        $this->destroyOnClose();
    }
}
laravel laravel-livewire
1个回答
0
投票

在我看来,你的 AccountMediumController 中有一个 findOrFail

这将引发模型未找到异常,导致 404

您也可以添加您的控制器代码吗?

© www.soinside.com 2019 - 2024. All rights reserved.