【发布时间】:2018-05-09 09:17:44
【问题描述】:
这是我的代码:
模型:
public static function myWishlist(){
$id = Auth::id();
$book_id = Wishlist::where('user_id', $id)->pluck('book_id');
return DB::table('books')->whereIn('id', $book_id)->get();
控制器:
public function index(Request $request)
{$books = Wishlist::myWishlist()->paginate(20);
return view('wishlistCRUD.index', compact('books'))
->with('i', ($request->input('page', 1) - 1) * 5);
但它没有向我展示所有指定的书籍,而是说:
方法分页不存在。
我也在模型中试过这个:
$book_id = Wishlist::where('user_id', $id)->pluck('book_id');
return Book::whereIn('id',$book_id)->get();
但是它返回一个集合,我不知道如何在我的视图中显示它。
【问题讨论】:
-
paginate()应该是查询上的方法,而不是结果集上的方法 -
代替
return DB::table('books')->whereIn('id', $book_id)->get()使用return DB::table('books')->whereIn('id', $book_id)->paginate(20) -
您已经在
->get()myWishlist(),paginate()处理查询。你有一个结果。