0
votes

I am trying to figure out why my pagination stops working as soon as I apply it to a user model. Here is the scenario: I am making a website which is a note takes, a user can take notes save them and all that. On a page /notes all notes made can be seen used for me to see weather each user is able to save notes. Pagination works there without a problem. Once I made a user Dashboard and I show notes they made with their user id I get this error:

Method links does not exist …\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php 96

I have no clue why this is happening. When I delete the code from view: {{$notebooks->links()}} - the error goes away and so does pagination. The moment I put it in the error happens all over again.

Here is the code from my view and controllers.

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);
    }
}

Dashboard View (with paginate links code):

@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

Just a reminder this works perfectly without the user model. I am lost as how to fit it. Using Laravel 5.5.26.

1

1 Answers

2
votes

Change the code to:

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

Also, you do not need to get a user from DB since you can access auth()->user() instance in a view.