Ad

Is It Better To Retrieve Data Through AJAX Or Returning It Alongside View From Controller (Laravel)?

- 1 answer

So the scenario is getting data we need from controller and using it in our view. But there are two options, you can have either this line in your "show" method:
[email protected]

return view('webpage');

And in the 'webpage' you can send an Ajax request to [email protected] and get that data. Or you can get the data from database in [email protected] and then send it alongside view like this:
[email protected]

return view('store', compact('store')); 

But which is the more efficient and secure way of doing this?

Ad

Answer

It really depends on what you're doing, if the data you're requesting and the process you're running takes a lot of time or in a future it would, ajax is the way to go, but if process is short and the requested data from your model is small, then you can request it on the same method that returns your view and send the data to it.

Ad
source: stackoverflow.com
Ad