Aggiunta di logica alle viste
Domanda
La funzione seguente è l'ID utente di ottieni e ottieni gli articoli dall'elenco degli orologi dell'utente.C'è un modo in cui posso aggiungere la seguente logica alla vista?La linea commentata ha la vista che sto cercando di passarlo a.
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);
}
. Soluzione
// 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);
.
Visualizza modello (account/watchlist.blade.php
):
@foreach ($categories as $category)
{{ $category->id }}
@endforeach
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow