如何用foreach制作几行?

我正在使用 Laravel 5.8,在我的控制器中,我显示了 4 个元素paginate():


public function index()

{

    $proyects = Proyect::latest()->paginate(4);

    return view('proyect.index', compact('proyects'));

}

在视图中,4 个元素在一行中,但我想为每行显示 2 个元素。


<div class="row">


<div class="col-10 col-lg-12 col-md-6">

    <div class="card-deck">

        @forelse($proyects as $proyect)


        <div class="card">

            <div class="card-body">

                <h5 class="card-title">{{ $proyect->title }}</h5>

                <h6 class="card-subtitle mb-2 text-muted">

                 {{ $proyect->descripcion }}</h6>

                <p class="card-text">You can use the cap image as an 

                    overlay for the body</p>

                <a href="{{ route('proyect.show', $proyect) }}" 

                    class="card-link">Ver mas</a>

            </div>

            <div class="card-footer">

                <small class="text-muted">

                 {{ $proyect->created_at->diffForHumans() }}</small>

            </div>

        </div>


        @empty


        <li> Empty </li>


        @endforelse

    </div>

</div>

如何每行显示 2 个项目而不是一行中的所有项目?


一只甜甜圈
浏览 178回答 2
2回答

森栏

public function index(){&nbsp; &nbsp; $proyects = Proyect::latest()->paginate(10);&nbsp; &nbsp; return view('proyect.index', compact('proyects'));}array_chunk 是一个 php 函数,可以拼接你的数组。@forelse(array_chunk($proyects->all(),2) as $rows)&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; @foreach($rows as $proyect)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-10 col-lg-12 col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-deck">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="card-title">{{ $proyect->title }}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6 class="card-subtitle mb-2 text-muted">{{ $proyect->descripcion }}</h6>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="card-text">You can use the cap image as overlay for the body</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{ route('proyect.show', $proyect) }}" class="card-link">Ver mas</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-footer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <small class="text-muted">{{ $proyect->created_at->diffForHumans() }}</small>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </div>&nbsp; &nbsp; @empty&nbsp; &nbsp; <div>Empty ...</div>@endforelse

临摹微笑

可能这是您的解决方案:<div class="row">@forelse($proyects as $proyect)<div class="col-6 col-lg-6 col-md-6">&nbsp; &nbsp; <div class="card-deck">&nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="card-title">{{ $proyect->title }}</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6 class="card-subtitle mb-2 text-muted">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{{ $proyect->descripcion }}</h6>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="card-text">You can use the cap image as an&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overlay for the body</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="{{ route('proyect.show', $proyect) }}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="card-link">Ver mas</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-footer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <small class="text-muted">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{{ $proyect->created_at->diffForHumans() }}</small>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; @empty&nbsp; &nbsp; &nbsp; &nbsp; <li> Empty </li>&nbsp; &nbsp; </div>@endforelse</div>
打开App,查看更多内容
随时随地看视频慕课网APP