【问题标题】:Laravel ForEach Into Separate DIVsLaravel ForEach 分成单独的 DIV
【发布时间】: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


    【解决方案1】:

    由于您传递的是文章请求,因此您可以使用您声明的 $article->type 属性来调用 css div 类。

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

    然后在您的 app.css(或 less/scss 文件)中创建所需的颜色:

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

    【讨论】:

      猜你喜欢
      • 2018-04-21
      • 2016-05-09
      • 2015-11-24
      • 2018-07-08
      • 2012-01-17
      • 2014-05-28
      • 2014-07-29
      • 1970-01-01
      • 2015-10-22
      相关资源
      最近更新 更多