使用控制器中的 foreach 循环将数据返回给 Blade

问题描述

我有分类和帖子。我想让每个帖子都排列在彼此的类别中。以下是我在控制器中尝试的代码。然后我用 foreach 循环调用 Blade。

$smallpostsWithCat = array();
$postsWithCat = array();

$postsWithCatQuery = DB::table('posts')
    ->join('post_translations','posts.id','post_translations.post_id')
    ->join('language','post_translations.language_id','language.id')
    ->where('language.local',App::getLocale())
    ->where('big_thumbnail',1)->latest('post_date')->limit(1)->get();

$smallpostsWithCatQuery = DB::table('posts')
    ->join('post_translations','post_translations.post_id','posts.id')
    ->join('language',null)->latest('post_date')->limit(2)->get();

foreach ($cate as $item) {
    foreach ($postsWithCatQuery as $data) {
        if ($item->category_id == $data->category_id)
            $postsWithCat[] = $postsWithCatQuery;
    }
    foreach ($postsWithCatQuery as $data) {
        if ($item->category_id == $data->category_id)
            $smallpostsWithCat[] = $smallpostsWithCatQuery;
    }

}
       @foreach($postsWithCat as $post_b)
                                <!-- Small Post Start -->
                                    <div class="post sports-post post-separator-border">
                                        <div class="post-wrap">

                                            <!-- Image -->
                                            <a class="image" href="{{route('front.post_view',[$post_b->post_id])}}"><img
                                                    src={{$post_b->image}}
                                                        alt="post"></a>

                                            <!-- Content -->
                                            <div class="content">

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)