Laravel Pgination在foreach之后放置链接时不起作用

时间:2018-12-31 11:45:56

标签: laravel pagination

我正在使用laravel 5.2,而且我面临分页链接的一些问题。当我添加此代码时:

{{ $wachat->links() }}

在foreach之后,我收到此错误:

Call to undefined method Illuminate\Database\Query\Builder::links() (View: /Applications/MAMP/htdocs/kc/kyo-webservice/resources/views/wechat/show.blade.php)

在我的控制器中,我正在使用以下代码:

public function wechatshows($d1, $id, $name, Request $request)
{
    $users = Keyuser::where('imception_id', '=', $d1)->first();
    $user = UserKey::where('id', '=', $id)->first();
    if ($name == "all") {
        $wachat = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
    } else {
        $wachat = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
    }

    $wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
    return view('wechat.show', ['wachat' => $wachat, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}

我在自己的视图中使用此代码:

            <tbody id="chatListBody" style="overflow-y: scroll;">
                    @foreach ($wachat as $wachat)
                    <tr>
                    <input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
                        <td>{{ $wachat->id }}</td>
                        <td>{{ $wachat->wxid }}</td>
                        <td>{{ $wachat->username }}</td>
                        @if ($wachat->type === 'image')
                        <td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
                                data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
                        @elseif ($wachat->type === 'video')
                        <td><a href="{{ url('/images/'.$wachat->imgPath.'') }}">{{$wachat->imgPath}}</a></td>
                        @elseif ($wachat->type === 'audio')
                        <td> <a href="{{ url('/images/'.$wachat->imgPath.'') }}">{{$wachat->imgPath}}</a> </td>
                        @elseif ($wachat->type === 'text')
                        <td>{{$wachat->message}}</td>
                        @else
                        <td>no data</td>
                        @endif
                        <td>{{ $wachat->created_at }}</td>
                    </tr>
                    @endforeach
                </tbody>
                {{ $wachat->links() }}
            </div>
        </table>

当我将{{$ wachat-> links()}}放在foreach上方时,它正在工作,我不知道为什么会发生。

2 个答案:

答案 0 :(得分:1)

也许$wachat@foreach($wachat as $wachat)内部被覆盖。因此,找不到links()方法。

您应该使用@foreach($wachat as $singleWachat)或根据您的方便使用。

答案 1 :(得分:0)

当您遍历集合时,您的集合成为对象。在循环之后,执行{{$ wachat-> links()}}。您的代码尝试对对象进行分页并显示错误。 为避免这种问题,请尝试使用集合变量名称的复数形式和对象名称的单数形式。

public function wechatshows($d1, $id, $name, Request $request)
{
    $users = Keyuser::where('imception_id', '=', $d1)->first();
    $user = UserKey::where('id', '=', $id)->first();
    if ($name == "all") {
        $wachats = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
    } else {
        $wachats = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
    }

    $wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
    return view('wechat.show', ['wachats' => $wachats, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}

 <tbody id="chatListBody" style="overflow-y: scroll;">
                    @foreach ($wachats as $wachat)
                    <tr>
                    <input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
                        <td>{{ $wachat->id }}</td>
                        <td>{{ $wachat->wxid }}</td>
                        <td>{{ $wachat->username }}</td>
                        @if ($wachat->type === 'image')
                        <td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
                                data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
                        @elseif ($wachat->type === 'video')
                        <td><a href="{{ url('/images/'.$wachat->imgPath.'') }}">{{$wachat->imgPath}}</a></td>
                        @elseif ($wachat->type === 'audio')
                        <td> <a href="{{ url('/images/'.$wachat->imgPath.'') }}">{{$wachat->imgPath}}</a> </td>
                        @elseif ($wachat->type === 'text')
                        <td>{{$wachat->message}}</td>
                        @else
                        <td>no data</td>
                        @endif
                        <td>{{ $wachat->created_at }}</td>
                    </tr>
                    @endforeach
                </tbody>
                {{ $wachats->links() }}
            </div>
        </table>