将Laravel 5.5方法链接应用于用户模型时,不存在(分页)



我试图弄清楚为什么我的分页将其应用于用户模型后立即停止工作。这是场景:我正在制作一个注释的网站,用户可以做笔记,保存它们以及所有这些。在页面/笔记上,所有笔记都可以看见我可以看到每个用户能够保存音符的天气。分页在那里无问题。一旦我制作了用户仪表板,我就会显示他们用用户ID做出的注释,我会遇到此错误:

方法链接不存在 … vendor laravel framework src inluminate support traits macroable.php 96

我不知道为什么会发生这种情况。当我从视图中删除代码时:{{$ nocebooks-> links((}} - 错误消失了,分页也会消失。当我把它放入错误的那一刻再次发生。

这是我的视图和控制器的代码。

dashboardController:

    public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        $notebooks = DB::table('notebooks', $user->notebooks)->paginate(4);
        return view('dashboard')->with('notebooks', $user->notebooks);
    }
}

仪表板视图(带有分页链链接代码(:

@extends('layouts.app')
@section('content')
       <!-- Main component for call to action -->
       <div class="container text-center">
           <!-- heading -->
           <h1 class="pull-xs-left">Your Dashboard</h1>
           <br>
           <div class="pull-xs-right">
               <a class="btn btn-primary" href="/notebook/create" role="button"> New Note +</a>
           </div>
            <div class="pull-xs-right">
               <a class="btn btn-primary" href="/contacts/create" role="button"> New Contact +</a>
           </div>
           <div class="pull-xs-right">
   <!--the dropdown menu -->
                   <div class="dropdown show">
                           <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                             Sort Notebooks By
                           </a>
                           <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                             <a class="dropdown-item" href="{{action('NotesController@ascendingId')}}">Note Number A - Z</a>
                             <a class="dropdown-item" href="{{action('NotesController@descendingId')}}">Note Number Z - A</a>
                             <a class="dropdown-item" href="{{action('NotesController@descendingTime')}}">Latest Time Posted</a>
                             <a class="dropdown-item" href="{{action('NotesController@ascendingTime')}}">Oldest Time Posted</a>
                           </div>
                   </div>
               </div>
           <div class="clearfix">
           </div>
           <br>
       @if(count($notebooks) > 0)
           @foreach ($notebooks as $notebook)
                <div class="col-sm-3 col-md-3 col-sm-6">
                   <div class="card" style="width: 26rem;">
                    <div class="card-header" >
                           Client: {{$notebook->client_name}}
                       </div>
                       <div class="card-block" >
                           <h4 class="card-title " style="height: 5rem;"><strong><a href="/notebook/{{$notebook->id}}" >{{$notebook->name}}</a></strong></h4>
                           <p class="card-text"  style="height: 7rem;">{{$notebook->note_description}}</p>
                       </div>
                       <ul class="list-group list-group-flush">
                           <li class="list-group-item"><b>Last Updated:</b> {{$notebook->updated_at->format('d-m-Y')}}</li>
                           <li class="list-group-item"> 
                               <a href="/notebook/{{$notebook->id}}/edit" class="btn btn-info">Edit Note</a>
                           </li>
                       </ul>
                       <div class="card-block">
                     {!!Form::open(['action' =>  ['NotesController@destroy', $notebook->id], 'method' => 'POST'])!!}
                           {{Form::hidden('_method', 'DELETE')}}
                           {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                     {!!Form::close()!!}
                   </div>
                 </div>
               </div>
           @endforeach
</div>
<!-- /container -->
           <hr>
            <div class="container text-center">
                    {{$notebooks->links()}}
                  </div>
           </div>
           @else
           <div>
               <div class="col-lg-12 no-notes">
                   <p>No notes found, go ahead and make your first note! :)</p>
                   <a class="btn btn-primary" href="/notebook/create" role="button"> Make My First Note</a>
               </div>
           </div>
       @endif

@endsection

只是一个提醒,没有用户模型就可以完美地工作。我迷失了如何适应它。使用Laravel 5.5.26。

将代码更改为:

$notebooks = auth()->user()->notebooks()->paginate(4);
return view('dashboard')->with('notebooks', $notebooks);

另外,您不需要从数据库获得用户,因为您可以在视图中访问auth()->user()实例。

最新更新