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

huangapple go评论73阅读模式
英文:

How to return images sequentially inside a "for loop"?

问题

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

我的图片表:

+----+------------------------------------+-------------------------+--------------+
| 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 |
+----+------------------------------------+-------------------------+--------------+


我的控制器:

```php
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">
            <!-- 第一张幻灯片图片 -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- 第二张幻灯片图片 -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- 第三张幻灯片图片 -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- 第四张幻灯片图片 -->
            {{ $blue->polymorphicRelationship->image1 }}
            <!-- 第五张幻灯片图片 -->
            {{ $blue->polymorphicRelationship->image1 }}
        </div>
        {{ $graytable->slice($i, 1)->first()?->content }}
    </div>
@endfor
英文:

My view displays collections from four different tables. $redtable is my longest collection.

@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

My image table:

+----+------------------------------------+-------------------------+--------------+
| 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 |
+----+------------------------------------+-------------------------+--------------+

My Controller:

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
        ]);
    }

How to display the images of the "Model Blue" table in the view.

I did a test using $picture->first()?->polymorphicRelationship()->first()->image, but it only displays the image of the first line of the table.

@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

答案1

得分: 0

假设关系被定义为多态一对多,你可以添加另一个循环。

此外,你可以用get($i)来替换slice($i, 1)->first()

@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
英文:

Assumming the relationship is defined as a polymorphic one to many, you can just add another loop.

Also you can replace slice($i, 1)-&gt;first() with get($i).

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

</details>



# 答案2
**得分**: 0

我认为要按顺序在视图中显示蓝色表格的图像,你需要分别检索蓝色表格每一行的图像。一种方法是遍历蓝色表格的行以及其他表格,并在单独的幻灯片中显示每一行的图像。

```php
@for ($i = 0; $i &lt; $red-&gt;count(); $i++)
    &lt;div class=&quot;general&quot;&gt;
        {{ $red-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
        {{ $green-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
        @if ($i &lt; $blue-&gt;count())
            &lt;div class=&quot;slide&quot;&gt;
                @foreach ($blue[$i]-&gt;polymorphicRelationship-&gt;images as $image)
                    &lt;img src=&quot;{{ $image-&gt;image }}&quot; alt=&quot;&quot;&gt;
                @endforeach
            &lt;/div&gt;
        @endif
        {{ $gray-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
    &lt;/div&gt;
@endfor
英文:

I think to display the images of the Blue table in the view sequentially, you need to retrieve the images for each row of the Blue table separately. One way to do this is to loop through the rows of the Blue table along with the other tables and display the images of each row in a separate slide.

@for ($i = 0; $i &lt; $red-&gt;count(); $i++)
    &lt;div class=&quot;general&quot;&gt;
        {{ $red-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
        {{ $green-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
        @if ($i &lt; $blue-&gt;count())
            &lt;div class=&quot;slide&quot;&gt;
                @foreach ($blue[$i]-&gt;polymorphicRelationship-&gt;images as $image)
                    &lt;img src=&quot;{{ $image-&gt;image }}&quot; alt=&quot;&quot;&gt;
                @endforeach
            &lt;/div&gt;
        @endif
        {{ $gray-&gt;slice($i, 1)-&gt;first()?-&gt;content }}
    &lt;/div&gt;
@endfor

huangapple
  • 本文由 发表于 2023年2月18日 03:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定