以下功能获取用户的ID并获取用户观察列表中的项目。有没有办法可以将以下逻辑添加到视图中?注释行认为我正试图将其传递给。
public function getWatchList() {
$user = User::find(Auth::user()->id);
$item = WatchList::where('user_id', '=', $user->id)->get();
foreach ($item as $i) {
$items = Catagory::where('id', '=', $i->item_id)->get();
foreach ($items as $item) {
echo $item->id;
}
}
// return View::make('account.watchlist')
// ->with('items', $items);
}
答案 0 :(得分:1)
// don't need that, unnecessary db call, twice..
// $user = User::find(Auth::user()->id);
// you can do this:
$categoriesIds = WatchList::where('user_id', '=', Auth::id())->lists('item_id');
// then
$categories = Category::whereIn('id', $categoriesIds)->get();
return View::make('account.watchlist')->with('categories', $categories);
查看模板(account/watchlist.blade.php
):
@foreach ($categories as $category)
{{ $category->id }}
@endforeach