简体   繁体   English

Eloquent Query Builder 中的 Laravel 分页

[英]Laravel pagination in Eloquent Query Builder

I need help to paginate a table that has a relationship with another using Laravel pagination.我需要帮助使用 Laravel 分页对与另一个表有关系的表进行分页。 I get an error that says...我收到一个错误,说...

too fewer argument to function函数的参数太少

Controller控制器

public function users()
{
    $users = User::join('customers', 'users.id', '=', 'customers.user_id')
        ->select('customers.*')->orderBy('customers.name', 'asc')
        ->paginate(2);

    return view('paginate', ['users' => $users]);
}

View看法

<div class="container">
    <table class="table">
        <thead>
        <tr>
            <th>Name</th>
            <th>Location</th>
            <th>Age</th>
        </tr>
        </thead>
        <tbody>
        @foreach ($users->customers() as $customer)
            <tr>
                <td>{{$customer->id}}</td>
                <td>{{$customer->location}}</td>
                <td>{{$customer->age}}</td>
            </tr>
        @endforeach
        </tbody>
    </table>
    {{$users->customers()->links()}}
</div>
<script type="text/javascript" src="{{asset('js/frontend_js/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{asset('js/frontend_js/bootstrap.min.js')}}"></script>

Relationship (between the two tables)关系(两个表之间)

public function customers()
{
    return $this->hasMany('App\Customer')->paginate(2);
}

public function users()
{
    return $this->belongsTo('App\User');
}

Replace this line替换这一行

{{$users->customers()->links()}}

with this有了这个

{{$users->links()}}

Remove pagination from this relation.从此关系中删除pagination

public function customers(){

       return $this->hasMany('App\Customer');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM