Laravel无法显示超过10,000条的mysql记录

时间:2018-12-11 08:58:53

标签: php mysql laravel

我无法从Laravel中的mysql显示超过10,000行的mysql行。

我已经阅读了有关使用块的信息,但是我无法完成这项工作,我认为我可以使用datatable javascript完成此操作,这显示每页最多10条记录,这是我想要的,但是它尝试加载首先将整个10,000条记录放入每组10条中,然后将我的代码放置在下面。

预先感谢

我的页面控制器如下:

    public function index($bzname)
{
    $user = Auth::guard('business')->user();
    $business_admin= BusinessAdmin::where('user_id', $user->id)->first();
    $business = Business::where('business_name', $bzname)->first();

    $users = User::join('business_users', 'users.id', '=', 'business_users.user_id')
        ->leftJoin('user_login_times', 'users.id', '=', 'user_login_times.user_id')
        ->leftJoin('social_logins', 'users.id', '=', 'social_logins.user_id')
        ->where('business_users.business_id', $business_admin->business_id)
        ->select('users.*', 'social_logins.provider', 'social_logins.social_id', 'user_login_times.login_time')
        ->orderBy('login_time', 'desc')->get();

    $users = $users->unique('id');

    // Get User Roles
    $roles = Role::all();

    $current_year = date('Y');

    $data = [
        'user' => $user,
        'users' => $users,
        'roles' => $roles,
        'bzname' => $bzname,
        'current_year' => $current_year,
        'bzadmin' => $business->owner_name,
    ];

    return View('pages.business_admin.users.show-users', $data);
}

我的用户页面显示为:

  <tbody id="users_table">
    @foreach($users as $user)
     <tr>
      <td>{{$loop->index + 1}}</td>
       <td><img src="@if($user->image_url) {{ $user->image_url }} @else /images/faces/empty.png @endif" class="mr-2" alt="image"></td>
        <td class="hidden-xs">{{$user->first_name}}</td>
        <td class="hidden-xs">{{$user->last_name}}</td>
        <td class="hidden-xs" width="100px"><a href="mailto:{{ $user->email }}"          title="email {{ $user->email }}">{{ $user->email }}</a>
         </td>
         <td style="text-transform: capitalize;">{{ $user->gender }}</td>
          <td>{{ $current_year - $user->year }}</td>
          <td style="text-transform: capitalize;">{{ $user->country }}</td>
          <td>
          @if($user->provider == null)
          Email
          @else
         {{ $user->provider }}
         @endif
         </td>
         <td>{{ $user->login_time }}</td>


         <td class="actions" width="180px">
         <a class="btn btn-sm btn-success" href="{{ URL::to('/business/'.$bzname.'/admin/users/' . $user->id) }}"
                                           data-toggle="tooltip" title="View">
                                          {!! trans(('usersmanagement.buttons.show'))!!}
                                        </a>



                                    </td>
                                </tr>
                            @endforeach
                            </tbody>




<script type="text/javascript">
    $(function() {
        $('#user-listing').dataTable({
            dom: 'Bfrtip',
            buttons: [
                'copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5'
            ],
            "iDisplayLength": 10,
        });
        $('#user-listing_filter').attr('style', 'display: none');
    });
</script>

4 个答案:

答案 0 :(得分:1)

不要。

表本身中的1万行会导致浏览器崩溃。

尤其是在表格中。回流本身将永远需要!

您的服务器可能内存不足。

如果每秒收到多个请求,则每个下一个请求将花费越来越长的时间。

分页 https://laravel.com/docs/5.4/pagination

显示10,而不是显示1万行,或者在pagination上重新加载页面,或者使用datatables(例如)进行ajax分页。

答案 1 :(得分:0)

您可以像这样使用 chunking

DB::table('users')->orderBy('id')->chunk(10000, function ($users) {
    foreach ($users as $user) {
        //
    }
});

更多信息,请访问:Chunking Results in Laravel

答案 2 :(得分:0)

这里有很多记录,应该分页并限制记录。

$users = $users->paginate(50);

$users = $users->simplePaginate(50);

在刀片中,只需手动创建分页

{{ $users->links() }}

More details for pagination

在此处删除get():

 $users = User::join('business_users', 'users.id', '=', 'business_users.user_id')
->leftJoin('user_login_times', 'users.id', '=', 'user_login_times.user_id')
->leftJoin('social_logins', 'users.id', '=', 'social_logins.user_id')
->where('business_users.business_id', $business_admin->business_id)
->select('users.*', 'social_logins.provider', 'social_logins.social_id', 'user_login_times.login_time')
->orderBy('login_time', 'desc')->paginate(50);

希望这会有所帮助。

答案 3 :(得分:0)

我认为laratables很有用,如果您尝试在服务器端处理中使用数据表

安装

composer require freshbitsweb/laratables

创建仅用于获取数据的路由

public function mydata()
{
  return Freshbitsweb\Laratables\Laratables::recordsOf(YOUR_Model);
}

有关更多详细信息,laratables