I am using livewire modal from this repo: https://github.com/wire-elements/modal
So, I create an edit modal where it can also delete the item. Here is the modal look like
There are delete button. When I click that, its actually deleted. No error given, and the modal closed but after a second there is another pop up showing 404 not found
Even I cannot add another account, when I click a button to add another account it keeps showing 404 not found. The only way to solve this is to refresh the page then its working normally.
Here is the delete function from 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();
}
Here is the complete code if you wish to see it in full mode
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-dTH: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();
}
}
2
Answers
It looks to me you have a findOrFail inside your AccountMediumController
This will throw an model not found exception resulting in a 404
Could you add your controller code as well?
Yes, agree with @jasper. you have to mentioned the controller code so we can idenitfy the issue.
But i think after delete you should have to refresh the livewire component.