Return view from Ajax Form handler
Created 5 years ago by cynoHi, 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);
}
);
}
}
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.
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