Return view from Ajax Form handler
Created 5 years ago by cyno

Hi, How can I return a view to display after ajax form request? Do I use $builder->on(), or something else? FormHandler at the moment:

class ProductFormHandler {

use DispatchesJobs;

public function filter(FormBuilder $builder, ProductRepositoryInterface $products)
{
    $product = $products->findByFilters($builder->getFormValues()->all())->first();
    $view = view('myview', compact('product'))->render();

    $builder->on(
        'json_response',
        function (Collection $data) use ($view) {
            $data->put('test', $view);
        }
    );
}

}

finnito  —  5 years ago

Hey @cyno!

Could you show what your getting as a reponse at the moment or what errors you are encountering?

You are correct in using view()->render() to get the HTML for the view, so that’s great. There is this response on SO which covers a similar problem, so that might be of help.

https://stackoverflow.com/a/28635193

cyno  —  5 years ago

Thank you for your answer @finnito! I've seen this SO response, if i do this its just returns nothing, the response is blank. If i use Collection in the callback everything is fine, without the view, but with view included, it just give blank return as well. Also if i dd() the view instance its fine, but if i call render() its just blank again. I dont see errors in any of the cases.

cyno  —  5 years ago

Maybe the problem is that i use blocks in the rendered view? I can provide some code in an hour.

finnito  —  5 years ago

Okay sounds great! It’s late here so I can check your code when I wake in the morning and do some testing myself if you can’t figure it out!