請教大大:
我有 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條查詢,如下圖:
刷新後 剩下 6條查詢,如下圖:
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');
}
請教前輩,如果想要將剩下的查詢,也都加入緩存中,應該怎麼寫?
預先謝謝熱心相助的前輩大大。謝謝!