Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a full page reload in Livewire

I use Livewire and Alpine with Laravel 8.

I have a page with a Datatable (jQuery) and a Bootstrap modal.
The table is filled with some data from a list of model instances.
When I click on a button in the table, it opens the modal and allows to edit the corresponding record.
This part is working as expected.

However, the Datatable library is much more to difficult to use with Livewire, so I decided to scope the entire Datatable in a <div> with wire:ignore attribute.
Because of that, If I want to refresh the Datatable when a modification has been made, I can't use the magic $refresh because the Datatable is currently scoped inside wire:ignore.

So I was thinking about making a full page reload after the edit is done, but I did not figure out how to do this, return redirect()->back() is not working...

This is what my save method looks like, which is called when an edit from the modal is done:

public function save()
{
    MyModel::where('id', $this->edited_id)->update([...]);
    $this->clearSelection();
    return redirect()->back(); // This is not working
}

And this is my table:

<div wire:ignore>
    <table class="dt-table table">
        <thead>
            <tr>
                <th>Actions</th>
                <th>id</th>
                <th>name</th>
                <th>other</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($models as $m)
                <tr>
                    <td>
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#mymodal">Edit</button>
                    </td>
                    <td>{{ $m->id }}</td>
                    <td>{{ $m->name }}</td>
                    <td>{{ $m->somedata }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>
like image 749
Marc Avatar asked Dec 13 '22 07:12

Marc


1 Answers

Livewire stores the original URL in the Referer header. You can use it to refresh the page:

return redirect(request()->header('Referer'));
like image 145
Sjors Ottjes Avatar answered Dec 24 '22 04:12

Sjors Ottjes