Alex Winter
Alex Winter

Reputation: 201

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.push(value.name);
      });

      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?

Upvotes: 0

Views: 401

Answers (1)

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

Upvotes: 2

Related Questions