【问题标题】:Undesirable duplicate content with double foreach in blade刀片中带有双 foreach 的不受欢迎的重复内容
【发布时间】:2017-11-06 01:00:13
【问题描述】:

如何确保在刀片中使用更多的 foreach,而内容不会重复多次?

    $review = Review::orderBy('created_at', 'desc')->paginate(9);
    $info_games = array();
    $games = array();
    foreach ($review as $value) {
        $info_games[]   =   InfoGames::where('id_game', $value->id_game)->first();
        $games[]    =   Games::where('id', $value->id_game)->first();
    }
    return view('front.pages.review.list', compact('review','info_games','games'));

还有我的刀:

@foreach ($review as $value)
@foreach ($info_games as $info_game)
    <div>
      <img src="https://gamelite.net/{{$info_game->image}}">
      <p>{!! str_limit($value->body, 150)  !!}</p>
    </div>
@endforeach
@endforeach
{!! $review->render() !!}

【问题讨论】:

标签: php laravel foreach blade


【解决方案1】:

使用 array_filter($array) 。它将返回 unique 。 在你的情况下

$info_games = array_filter($info_games);

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2010-09-07
    相关资源
    最近更新 更多