Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yansusanto's avatar

How to load view blade upon Ajax Success?

                $(document).ready(function(e) { 
                    $("#search").keyup(function() { 
                        $query = $(this).val(); 
                        $.ajax({ 
                            type: 'GET', 
                            url: 'dashboard',
                            data: {'q': $query},
                            dataType: 'json', 
                            success:function(data) { 
                                $.each(data.data, function(key, value) {
                                    $('#ajaxResults').html('
                    // is there a way to load an existing view blade here?
                ');
                                });
                            }       
                        }); 
                    }); 
                });

What is the best practice to load a view blade.php upon Ajax success. A view with simple table are fine but what if the table includes @foreach, an PATCH / DELETE form?

           <table class="table">
                <thead>
                    <tr>
                        <th>Status</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Joined</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($users as $user)
                    <tr> 
                        <td data-label="Status">
                            {{ Form::open(['method' => 'PATCH', 'action' => ['AdminController@updateStatus', $user->id]]) }}
                            <button type="submit" class="btn is-submit">
                                @if ($user->status == '0') 
                                <i class="fas fa-toggle-off fa-2 is-red"></i>
                                @else
                                <i class="fas fa-toggle-on fa-2 is-green"></i>  
                                @endif 
                            </button>
                            {{ Form::close() }}   
                        </td>                                
                        <td data-label="Name">
                            {{ $user->name }}
                        </td>
                        <td data-label="Email">
                            {{ $user->email }}
                        </td>
                        </td>
                        <td data-label="Joined" class="rtl">
                            {{ $user->created_at->diffForHumans() }}
                        </td>
                    </tr>                   
                    @endforeach
                </tbody>
            </table>

Any advise is appreciated! Thanks!

0 likes
6 replies
bwrice's avatar

You would have to send the template AFTER rendering it first.

Cronix's avatar
Cronix
Best Answer
Level 67

something like

return response()->json([
    'html' => view('your-table-view', compact('users'))->render()
]);

and then

success:function(data) { 
    $('#ajaxResults').html(data.html);
}

You only want the view to contain your table like you are showing, not an entire page like something extending a master template.

4 likes
yansusanto's avatar

Thanks, @Cronix. I tried your method and the AJAX stops working.

dashboard.blade.php where search results are displayed and within

@include('dashboard.admin')

and within admin.blade.php

@include('table.userTable')

So I'm assuming that I could load table.userTable.

Cronix's avatar
'html' => view('table.userTable', compact('users'))->render()

?

1 like
yansusanto's avatar

Ah, damn. Always make this kind of silly mistake. It should be table.userTable. It's all good now.

I didn't realize the method is that simple. All the things that I read and researched on stackoverflow is so scary.

Once again, thanks @Cronix for helping out.

Please or to participate in this conversation.