【问题标题】:i have an if statement but i have a problem我有一个 if 语句,但我有问题
【发布时间】:2021-01-13 16:38:15
【问题描述】:

所以我有一个问题,很难解释,但可以描述如下:

this is ouput if i paginate 10

上图,我分页10,从以下代码可以看出:

$list = Anime::where('status', 1)->paginate(10);

output-2 paginate 5

如果上图,我分页是5,从下面的代码可以看出:

$list = Anime::where('status', 1)->paginate(5);

显示如下代码图片(刀片文件):

<div class="flex-shrink-0 h-10 w-10">
 @if ($creator[$i]->profile_photo_path)
  @php
  $isi = $creator[$i]->profile_photo_path;
 @endphp
  <img class="h-10 w-10 rounded-full" src='{{ asset("storage/$isi") }}' alt="">
 @elseif ($creator[$i]->profile_photo_path == null || !$creator[$i]->profile_photo_path)
  <img class="h-10 w-10 rounded-full" src="{{ asset('storage/profile-photos/default.svg') }}"     alt="">
 @endif
</div>

以下是刀片文件的完整内容:

        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Public List Anime') }}
        </h2>
    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            
            <div class="flex flex-col">
                <div class="-my-2 overflow-x-hidden sm:-mx-6 lg:-mx-8">
                    <div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
                        <div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
                            <table class="min-w-full divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th scope="col" class="hidden md:inline-block px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                            Username
                                        </th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                            Title
                                        </th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                            Genre
                                        </th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                            Rating
                                        </th>
                                        <th scope="col" class="relative px-6 py-3">
                                            <span class="sr-only">Edit</span>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    @foreach ($list as $i => $l)
                                    <tr>
                                        <td class="hidden md:inline-block px-6 py-4 whitespace-nowrap">
                                            <div class="flex items-center">
                                                <div class="flex-shrink-0 h-10 w-10">
                                                    @if ($creator[$i]->profile_photo_path)
                                                    @php
                                                    $isi = $creator[$i]->profile_photo_path;
                                                    @endphp
                                                    <img class="h-10 w-10 rounded-full" src='{{ asset("storage/$isi") }}' alt="">
                                                    @elseif ($creator[$i]->profile_photo_path == null || !$creator[$i]->profile_photo_path)
                                                    <img class="h-10 w-10 rounded-full" src="{{ asset('storage/profile-photos/default.svg') }}" alt="">
                                                    @endif
                                                </div>
                                                <div class="ml-4">
                                                    <div class="text-sm font-medium text-gray-900">
                                                        {{ Illuminate\Support\Str::of($l->email)->title() }}
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="text-sm text-gray-900">{{ Illuminate\Support\Str::of($l->title)->title() }}</div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                @php $g = explode(',', $l->genre) @endphp
                                                {{ Illuminate\Support\Str::of($g[0])->title() }}
                                            </span>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            
                                            {{ $l->score }} <i class="fal fa-stars"></i>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                            <a href="{{ route('show', $l->id) }}" class="text-indigo-600 hover:text-indigo-900">Detail</a>
                                        </td>
                                    </tr>

                                    
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <span>
                {{ $list->links() }}
            </span>
        </div>
    </div>

这是控制器方法索引:

public function index()
    {
        $list = Anime::where('status', 1)->paginate(5);

        $creator = Anime::join('users', 'users.email', '=', 'animes.email')
        ->where('status', 1)
        ->get('users.profile_photo_path');

        return view('anime.publicList', compact('list', 'creator'));
    }

问题是,如果我将其分页为 5,那么当在第二页时,用户图像会变成管理员图像(与第一个图像分页 10 不同)

【问题讨论】:

  • 管理员图片和用户图片都是从users.profile_photo_path加载的吗?还是其中任何一个是默认图像?
  • admin 图片来自 users.profile_photo_path,但用户 img 来自默认图片,因为用户没有设置 img
  • 可能是缓存问题。重置缓存时会发生什么? (按住 Ctrl 并重新加载)或者如果您使用的是 chrome,在检查元素的网络选项卡中,禁用缓存。我认为值得一试

标签: php laravel if-statement pagination


【解决方案1】:

#按以下方式解决:

我尝试将变量 $creator 更改为分页(等同于 $ 日本动漫变量)

所以我改成如下内容:

$creator = Anime::join('users', 'users.email', '=', 'animes.email')
    ->where('status', 1)
    ->select('users.*')
    ->paginate(5);

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2017-02-11
    • 2014-01-23
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多