【发布时间】:2015-10-18 16:36:57
【问题描述】:
我正在开发一个项目,该项目将根据用户的兴趣显示文章。我有一个 user_table、follower_table 和 article_table。目前,我能够获取 user_id 并从 follower_table 和他们感兴趣的每篇文章中获取他们所有相关的兴趣。我使用 Foreach 循环浏览他们所有的文章。但是,现在我希望能够根据它们的类型将每篇文章放入不同的 div 中。为了能够有不同的尺寸和颜色。过滤 foreach 循环的最佳方法是什么?下面是我的代码。
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
用户控制器:
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
【问题讨论】:
标签: php twitter-bootstrap laravel for-loop masonry