duanci19881003 2015-04-14 22:26
浏览 364
已采纳

Laravel:需要解决foreach循环问题

What i'm trying to do is basically have the "latest" episodes show for a series that is has a status of "ongoing" below is the code i have so far.

The problem i a facing is that I can't seem to make the foreach loop for episodes work for the series. Wit hthe current code what it does is shows the same variables. Rather what i think is happening is that it loops the same query for each series so that the same variable pops up for each series.

Can anyone help me out here?

Also the way the episodes are linked is by using the title_id for the titles so in the table for episodes, they are liked by 'title_id', I wouldn't know what to do with that in this sequence though.

        <?php $titles = DB::table('titles')->whereNotNull('poster')->where('status', '=', 'ongoing')->orderBy('updated_at', 'desc')->limit(12)->get(); ?>
        @foreach ($titles as $title)

            <?php $episodes = DB::table('episodes')->orderBy('created_at', 'desc')->limit(1)->get(); ?>

            @foreach ($episodes as $episode)
            <figure class="col-lg-2 col-md-3 col-sm-4  pretty-figure">
                <div class="home-episode-number">
                    {{ $episode->episode_number }}
                </div>
                <div class="flip-containerw">
                    <div class="flipper">
                      <a href="{{ Helpers::episodeUrl($title->title, $episode->title_id, 'series', $episode->season_number, $episode->episode_number) }}"><img src="{{ $episode->poster ? $episode->poster : '/assets/images/noimageepisode.png' }}" alt="" class="img-responsive"></a>
                    </div>
                </div>
                <div class="home-anime-name">
                    <a href="{{ Helpers::episodeUrl($title->title, $episode->title_id, 'series', $episode->season_number, $episode->episode_number) }}" class="series-name-link"> {{ str_limit($title->title, 23, '...') }} </a>
                </div>
            </figure>
    @endforeach                 
@endforeach
  • 写回答

1条回答 默认 最新

  • duanjuan3931 2015-04-15 01:10
    关注

    You are not following some basic design patterns, like, for instance, the Model-View-Controller structure.

    MVC

    It's not good practice to have DB calls inside your view, wich you are doing. You should do it inside your model, or in a repository. And pass it trought the controller.

    You would avoid a lot of headache if you start using eloquent properly.

    Eloquent

    Now, answering your question:

    If you want to get the episode for the title in the loop, try using a where:

    $episodes = DB::table('episodes')->where('title_id,'=',$title->id)->orderBy('created_at', 'desc')->limit(1)->get();
    

    That query will retrieve just one episode (limit(1)).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化