Alex Winter Alex Winter - 9 months ago 89
Javascript Question

Ajax GET request Laravel 5.2

I have the following in my routes.php:

Route::get('/displayphotos', function()
return view('displayphotos')->with('photos', photo::all());

I basically have a javascript code section which initially uses the $photos that the displayphotos routes returns using photo::all()... This does work and everything.
I have this running on a 10000 interval.

setInterval(function() {
_.forEach({!! $photos !!}, function(value, key) {

photos_array = _.uniq(photos_array);

More code here...

}, 10000);

So essentially what I want to accomplish is having that $photos get refreshed in here when the interval kicks in it would grab the latest values from $photos which is stored in the database. I am assuming this means I need to make an ajax request somehow using GET? Would I need to add a controller method for this? or can it be done inline somehow?

Can anyone help with how I would accomplish what I am asking?


Use an AJAX request because php code executes in the server side and your UI is in the client side, so the operation all runs only once time.

You can try this:

 Route::get('/displayphotos', function()
   return json_encode(photo::all());

To display a JSON array and use a AJAX request to consume that every interval