php 如何在“for循环”中顺序返回图像?

ruarlubt  于 2023-02-18  发布在  PHP
关注(0)|答案(1)|浏览(98)

我的视图显示来自四个不同表的集合。$redtable是我最长的集合。

@for ($i = 0; $counter < $redtable->count(); $i++)
    <div>
        {{ $red->slice($i, 1)->first()?->content }}
        {{ $green->slice($i, 1)->first()?->content }}
        {{ $blue->slice($i, 1)->first()?->content }}
        {{ $gray->slice($i, 1)->first()?->content }}
    <div>
@endfor

我的图像表:

+----+------------------------------------+-------------------------+--------------+
| id | image                              | imageable_type          | imageable_id |
+----+------------------------------------+-------------------------+--------------+
|  1 | /path/redtable/image.png           | App\Models\Red          |            1 |
|  2 | /path/greentable/image.png         | App\Models\Green        |            1 |
|  3 | /path/blue/image1.png              | App\Models\Blue         |            1 |
|  4 | /path/blue/image2.png              | App\Models\Blue         |            1 |
|  5 | /path/blue/image3.png              | App\Models\Blue         |            1 |
|  6 | /path/blue/image4.png              | App\Models\Blue         |            1 |
|  7 | /path/blue/image5.png              | App\Models\Blue         |            1 |
|  8 | /path/gray/image.png               | App\Models\Gray         |            2 |
+----+------------------------------------+-------------------------+--------------+

我的控制者:

class ColorsController extends Controller
{
    public function index()
    {
        $red = \App\Models\Red::with('polymorphicRelationship')->limit(3)->orderby('id', 'desc')->get();
        $green = \App\Models\Green::with('polymorphicRelationship')->limit(3)->orderby('id', 'desc')->get();
        $blue = \App\Models\Blue::with('polymorphicRelationship')->limit(3)->orderby('id', 'desc')->get();
        $gray = \App\Models\Gray::with('polymorphicRelationship')->limit(3)->orderby('id', 'desc')->get();
        
        return view('home.colors', [
            'red' => $red,
            'green' => $green,
            'blue' => $blue,
            'gray' => $gray
        ]);
    }

如何在视图中显示“Model Blue”表的图像。
我使用$picture->first()?->polymorphicRelationship()->first()->image做了一个测试,但是它只显示表第一行的图像。

@for ($i = 0; $counter < $red->count(); $i++)
    <div class="general">
        {{ $red->slice($i, 1)->first()?->content }}
        {{ $green->slice($i, 1)->first()?->content }}
        {{ $blue->slice($i, 1)->first()?->content }}
        <div class="slide">
            <!-- First slide image -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- Second slide image -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- Third slide image -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- Fourth slide image -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- Fifth slide image -->
            {{ $blue->polymorphicRelationship->image1 }}
        </div>
        {{ $graytable->slice($i, 1)->first()?->content }}
    </div>
@endfor
z9gpfhce

z9gpfhce1#

假设关系被定义为多态的一对多关系,您可以添加另一个循环。
您也可以将slice($i, 1)->first()替换为get($i)

@for ($i = 0; $counter < $red->count(); $i++)
    <div class="general">
        {{ $red->get($i)?->content }}
        {{ $green->get($i)?->content }}
        {{ $blue->get($i)?->content }}
        <div class="slide">
            @foreach ($blue->polymorphicRelationship as $image)
                {{ $image->image }}
            @endforeach
        </div>
        {{ $graytable->get($i)?->content }}
    </div>
@endfor

相关问题