現在の投稿を基準に、簡単に【次へ進む】【前に戻る】ボタンを実装したいです。
PHP(コントローラー)
1 public function company_single(User $user,Post $post)2 {3 $auther = User::find($post->user_id);4 $items = Post::where('user_id', $auther->id)->get();5 $previous=post::where('id', '<', $items->id)->orderBy('id', 'desc')->first();6 $next=Post::where('id', '>', $items->id)->orderBy('id')->first();7 return view('/company-single',compact('user','post','auther','previous','next'));8 }
PHP(ビュー)
1 <div class="pagination">2 <div>3 @if (isset($previous))4 <a href="{{ route('company-single', $previous->id) }}"></a>5 @endif6 </div>7 <div>8 @if (isset($next))9 <a href="{{ route('company-single', $next->id) }}"></a>10 @endif11 </div>12 </div>
上記コードだと、以下の箇所で
php(コントローラー)
1$previous=post::where('id', '<', $items->id)->orderBy('id', 'desc')->first();
以下のエラーが出ます。
Property [id] does not exist on this collection instance.
postモデルには固有のidがDB、idカラムに保存されているのに、なぜpostモデルにidがありません。というエラーになってしまうのでしょうか?
0 コメント