From b80d74bae1f4e1490d085bc9c9230ce12c1120e7 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sun, 14 Apr 2019 16:46:20 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=84=E3=81=84=E3=81=AD=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=81=AE=E8=A1=A8=E7=A4=BA?= =?UTF-8?q?=E3=82=92=E7=9B=B4=E8=BF=9110=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E3=81=BF=E3=81=AB=E7=B5=9E=E3=82=8A=E3=81=93?= =?UTF-8?q?=E3=82=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Ejaculation.php | 27 +++++++++++++++++++-------- app/Like.php | 3 +++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/Ejaculation.php b/app/Ejaculation.php index d0a58f7..bc91288 100644 --- a/app/Ejaculation.php +++ b/app/Ejaculation.php @@ -6,10 +6,11 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; +use Staudenmeir\EloquentEagerLimit\HasEagerLimit; class Ejaculation extends Model { - // + use HasEagerLimit; protected $fillable = [ 'user_id', 'ejaculated_date', @@ -48,10 +49,15 @@ class Ejaculation extends Model if (Auth::check()) { // (ejaculation_id, user_id) でユニークなわけですが、サブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね return $query - ->with(['likes.user' => function ($query) { - $query->where('is_protected', false) - ->orWhere('id', Auth::id()); - }]) + ->with([ + 'likes' => function ($query) { + $query->latest()->take(10); + }, + 'likes.user' => function ($query) { + $query->where('is_protected', false) + ->orWhere('id', Auth::id()); + } + ]) ->withCount([ 'likes', 'likes as is_liked' => function ($query) { @@ -60,9 +66,14 @@ class Ejaculation extends Model ]); } else { return $query - ->with(['likes.user' => function ($query) { - $query->where('is_protected', false); - }]) + ->with([ + 'likes' => function ($query) { + $query->latest()->take(10); + }, + 'likes.user' => function ($query) { + $query->where('is_protected', false); + } + ]) ->withCount('likes') ->addSelect(DB::raw('0 as is_liked')); } diff --git a/app/Like.php b/app/Like.php index f4a6061..b1fa673 100644 --- a/app/Like.php +++ b/app/Like.php @@ -3,9 +3,12 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Staudenmeir\EloquentEagerLimit\HasEagerLimit; class Like extends Model { + use HasEagerLimit; + protected $fillable = ['user_id', 'ejaculation_id']; public function user()