0
votes

This is my function to delete a search record from the database. I expect the DB changes to be rendered without a page refresh. and that's y I use livewire. but after clicking delete, the page is updated only after refresh. the dd results don't show updated results.

public function deleteSearch($search) {
    $empsearch = Auth::user()->mySearches->find($search);
    $empsearch->delete();
    $this->skills = Auth()->user()->mySearches;
    dd($this->skills);
}

Blade file

@if (count($skills) > 0)
    @foreach ($skills as $search)
        <div class="el-tag" >
            <a href="/search?query={{ $search->name }}">
                {{ $search->name }}
            </a>
            |
            <a onclick="return confirm('Are you sure?')" wire:click.prevent="deleteSearch({{ $search->search_id }})" >
                <i class="pl-1 icon el-icon-delete"></i>
            </a>
        </div>
    @endforeach
@endif

These are the mount and render methods

public $preference;
public $skills;

public function mount($preference) {
    $this->preference = $preference;
    $this->skills = Auth()->user()->mySearches;
}

public function render() {
    return view('livewire.preferences');
}
2
Can you provide the blade file for your component.Peppermintology
@Peppermintology please check my blade file. thanksMohammed Sabeel
what version of livewire are you using 1x or 2xkenean50

2 Answers

0
votes

In the render method, the return statement is returning complete HTML DOM (i.e. return view('livewire.preferences') ) due to which the changes are taking place/reflecting only after the page refresh/reload.

Solution:-

  1. Make the delete request with the help of ajax
  2. Then on success of ajax request, update the main intendent DOM (i.e. the body part) so that the changes will be reflected without page refresh.
0
votes

after delete statement try this for get fresh data from records

$this->skills = Auth()->user()->mySearches->refresh();

UPDATED Ok maybe must be only like this:

$this->skills->refresh();