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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line