M
M
mrSeller2018-02-13 01:51:29
Laravel
mrSeller, 2018-02-13 01:51:29

Is it possible to return JSON with an array in the response, which will contain HTML elements from views?

The page is first loaded by the server, and later (filters or switching pagination) it switches to AJAX.
Accordingly, when components are rendered in the first case, we can directly access the methods specified in the model in the template and successfully extract as much information as we like.
For an AJAX request, we can immediately get all the necessary "chains" of data, but then the nesting of the object grows and rendering such a component in JS is extremely inconvenient.
Is it possible to access the view in the controller and render the HTML of the components, and then pass them back in the form of a JSON object?
The answer is needed in JSON , because there and the status, and the text, and other necessary data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mrSeller, 2018-02-13
@mrSeller

To render the desired view without passing it in the response, you just need to call the render () method

view('components.component', ['users' => $users])->render()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question