Auto update Page in Laravel

user9332352 Source

I am new to Laravel and I am making a page admin.blade.php which will show the data of the user from the database and update automatically when the new user registered, that's not the concern. My point is to update admin page and in doing so I am using jQuery which will take data from demo.admin.php and update the admin page.

demo.blade.php

<table class="table table-bordered" id="result">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
    @foreach($users as $value)
        <tr>
            <td>{{ $value->id }}</td>
            <td>{{ $value->name }}</td>
            <td>{{ $value->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

admin.blade.php

<script>
 $(document).ready(function() {
  setInterval(function() {
   $('#res').load('demo.blade.php');
  }, 100);
 });
</script>

DemoController

public function index()
{
    $users = DB::table('users')->select('id','name','email')->get();
    return view('demo')->with('users', $users);
}

AdminController

public function index()
{
    // return view('admin');
    $users = DB::table('users')->select('id','name','email')->get();
    return view('admin')->with('users', $users);
}

Plz, help me out. When I open admin page. the table is not showing.

phpjquerylaravellaravel-5

Answers

answered 5 days ago waterloomatt #1

As others have pointed out in the comments you can't point to a view because it isn't a valid URL. jQuery's load() method expects a valid URL pointing to a resource. http://api.jquery.com/load/

Assuming your JS is embedded in a Blade file you can use {{ action('[email protected]') }}.

<script>
 $(document).ready(function() {
  setInterval(function() {
   $('#res').load('{{ action('[email protected]') }}');
  }, 5000);
 });
</script>

comments powered by Disqus