代码之家  ›  专栏  ›  技术社区  ›  Nilay Singh

在foreach之后放置link时,laravel pgination不工作

  •  0
  • Nilay Singh  · 技术社区  · 6 年前

    我使用的是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 回复  |  直到 6 年前
        1
  •  1
  •   Kul    6 年前

    也许, $wachat 正在进行内部覆盖 @foreach($wachat as $wachat) . 所以,它找不到 links() 方法。

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

        2
  •  0
  •   Md.Sukel Ali    6 年前

    当循环遍历集合时,集合将成为对象。循环后当你做{{$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>