Search code examples
ajaxlaravelworkflownette

How to redraw html content via Ajax in Laravel - nette snippets


I'm looking for some unobtrusive ajax solution for Laravel.

for example Nette Framework has implemented own ajax workflow to do this.

but laravel doesn’t provide anything like this…

Edit: Of course, i know that i can do that with jQuery and JsonResponse but question is if there sofiscitated solution


Solution

  • You are looking for https://github.com/whipsterCZ/laravel-ajax

    It does exactly what you want and much more!

    I assume that you want to send some ajax request and you want to redraw some updated content after response

    its quite simple Blade Template

    @section('dynamic')
       <div id='greeting'>Hello {{name}}</div>
    @endSection()    
    <a href="/changeName?name=Láďo" class="ajax"></a>
    

    Laravel action

    public function changeName(Request $request, Ajax $ajax) {
        return $ajax
            ->redrawSection('dynamic')
            ->view('welcome',  ['name'=>$request->get('name')] )
    }
    

    You don't have to render whole page, you can just replace or append some html by ID.. it can do almost everything :)

    public function redrawPartial(Request $request, Ajax $ajax) {
        return $ajax
            ->redrawView('greeting')
        //  ->appendView('greeting') 
            ->view('partials._greeting',  ['name'=>$request->get('name')] )
    }