在刀片中使用双重foreach的不需要的重复内容

时间:2017-06-05 09:30:07

标签: php laravel foreach blade

我怎样才能确保在我的刀片中使用更多的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() !!}

1 个答案:

答案 0 :(得分:0)

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

$info_games = array_filter($info_games);