Laravel中的自动更新页面 [英] Auto update Page in Laravel

查看:432
本文介绍了Laravel中的自动更新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,我正在制作一个页面admin.blade.php,该页面将显示数据库中用户的数据,并在新用户注册后自动更新,这无关紧要.我的意思是要更新管理页面,这样做是使用jQuery,它将从demo.admin.php中获取数据并更新管理页面.

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,帮帮我.当我打开管理页面时.该表未显示.

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

推荐答案

正如其他人在注释中指出的那样,您不能指向view,因为它不是有效的URL. jQuery的load()方法需要一个指向资源的有效URL. http://api.jquery.com/load/

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/

假设您的JS嵌入在Blade文件中,则可以使用{{ action('DemoController@index') }}.

Assuming your JS is embedded in a Blade file you can use {{ action('DemoController@index') }}.

<script>
 $(document).ready(function() {
  setInterval(function() {
   $('#res').load('{{ action('DemoController@index') }}');
  }, 5000);
 });
</script>

这篇关于Laravel中的自动更新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆