iT邦幫忙

0

Laraver paginate 分頁 Cache:: 語法

  • 分享至 

  • xImage

請教大大:
我有 index.blade.php 以及 IssuesController.php 分別如下:
index.blade.php 的片段:


@foreach($issues as $issue)
<li class="am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left">
    <div class="am-u-sm-2 am-u-md-1 am-list-thumb">
        <a href="{{ route('issues.show', $issue->id) }}">
            <img src="{{$issue->user->avatar()}}" alt=""/>
        </a>
    </div>

    <div class="am-u-sm-7 am-u-md-9 am-list-main">
        <h3 class="am-list-item-hd">
            <a href="{{ route('issues.show', $issue->id) }}" class=""> {{ $issue->title }}</a>
        </h3>

        <div class="am-list-item-text">
            <span class="am-badge am-badge-secondary am-radius">read</span>
            <span class="meta-data">{{ $issue->user->name }}</span>
            {{ $issue->created_at->diffForHumans() }}
        </div>
    </div>
    <div class="am-u-sm-3 am-u-md-2 issue-comment-count">
        <span class="am-icon-comments"></span>
        <a href="{{ route('issues.show', $issue->id) }}">{{ $issue->comments->count() }}</a>
    </div>
</li>
@endforeach

IssuesController.php 的片段:


    public function index()
    {
        $page = request()->page ?? 1;
        $key = 'issues.paginate.'.$page;
        $issues = Cache::rememberForever($key, function () {
            return Issue::paginate(3);
        });
        return view('issues.index', compact('issues'));
    }

當我首次拜訪網頁時有8條查詢,如下圖:
https://ithelp.ithome.com.tw/upload/images/20200429/20121754NnkO8GBa8Q.png

刷新後 剩下 6條查詢,如下圖:

https://ithelp.ithome.com.tw/upload/images/20200429/20121754Kk6CTr5fYV.png

user、issue、comment 三者間的關係
use.php

    public function issues()
    {
        return $this->hasMany('App\Models\Issue');
    }

    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }

issue.php

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

comment.php

    public function issue()
    {
        return $this->belongsTo(Issue::class);
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

請教前輩,如果想要將剩下的查詢,也都加入緩存中,應該怎麼寫?
預先謝謝熱心相助的前輩大大。謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
tomusa
iT邦見習生 ‧ 2022-11-02 17:03:09

感謝您分享這些有用的信息,所以我知道如何對系統進行編程以使其不會失敗。 krunker

我要發表回答

立即登入回答