Question

The function below get's a user's id and get's the items from the user's watch list. Is there a way that I can add the following logic to a view? The commented line has the view that I'm trying to pass it to.

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);
}
Was it helpful?

Solution

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

View template (account/watchlist.blade.php):

@foreach ($categories as $category)
   {{ $category->id }}
@endforeach
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top