我试图找出为什么我的分页在应用到用户模型后停止工作的原因。这是一个场景:我正在建立一个笔记记录的网站,用户可以记录保存它们以及所有这些。在页面/注释上可以看到所有注释用于查看每个用户能够保存笔记的天气。分页在那里工作没有问题。一旦我创建了一个用户仪表板,我用他们的用户ID显示他们做的笔记,我得到了这个错误:
方法链接不存在 ... \供应商\ laravel \框架的\ src \照亮的\ Support \特征\ Macroable.php 96
我不知道为什么会这样。当我从视图中删除代码时:{{$ notebooks-> 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。
答案 0 :(得分:2)
将代码更改为:
$notebooks = auth()->user()->notebooks()->paginate(4);
return view('dashboard')->with('notebooks', $notebooks);
此外,您无需从DB获取用户,因为您可以在视图中访问auth()->user()
实例。