2015-07-28 24 views
1

我正在開發一個基於用戶興趣顯示文章的項目。我有一個user_table,follower_table和article_table。目前,我能夠獲取user_id,並從follower_table中獲取所有相關興趣,並根據他們的興趣生成每篇文章。我使用Foreach循環遍歷所有文章。但是,現在我希望能夠根據其類型將每篇文章放入不同的div中。爲了能夠有不同的尺寸和顏色。過濾foreach循環的最佳方法是什麼?以下是我的代碼如下。Laravel ForEach進入單獨的DIV

follower_table設置:

user_id:(this is the person/interest you follow) 

follower_id: (this is your id); 

article_table設置:

id 

user_id 

type: (music,fashion,events, dance) 

title 

body 

photo 

的UserController中:

class UserController extends Controller 

{

public function index() 

{ 

    $ilike = Auth::id(); 
    $ifollow = DB::table('followers')->where('follower_id', $ilike)->lists('user_id'); 
    $articles = DB::table('articles') 
     ->whereIn('user_id', $ifollow)->get(); 
    //$articles = DB::table('followers')->lists('title', 'name'); 
    //$articles = DB::table('followers')->where('follower_id', $ilike)->pluck('user_id'); 
    //$articles = Article::all(); 
    return view('pages.user', compact('articles')); 
} 

}

user.blade.php:

<div class = "grid"> 

            @foreach($articles as $article) 

              <div class="grid-item "> 
               <img src="/image/article/detail/{{$article->id}}.jpg"/> 
              </div> 

@endforeach

回答

1

因爲你是傳遞一個商品請求,您可以使用$物品─>類型的屬性,你宣稱,使用它調用css div類。

@foreach($articles as $article) 
     <div class="grid-item {{$article->type}}"> 
     <img src="/image/article/detail/{{$article->id}}.jpg"/> 
     </div> 

然後創建你的app.css所需的顏色(以下/ SCSS文件):

.music { 
} 
.fashion{} .events{} .dance{};