いいねしたユーザーの表示を直近10ユーザーのみに絞りこむ

This commit is contained in:
shibafu 2019-04-14 16:46:20 +09:00
parent 9b95f3a8b8
commit b80d74bae1
2 changed files with 22 additions and 8 deletions

View File

@ -6,10 +6,11 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Staudenmeir\EloquentEagerLimit\HasEagerLimit;
class Ejaculation extends Model class Ejaculation extends Model
{ {
// use HasEagerLimit;
protected $fillable = [ protected $fillable = [
'user_id', 'ejaculated_date', 'user_id', 'ejaculated_date',
@ -48,10 +49,15 @@ class Ejaculation extends Model
if (Auth::check()) { if (Auth::check()) {
// (ejaculation_id, user_id) でユニークなわけですが、サブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね // (ejaculation_id, user_id) でユニークなわけですが、サブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね
return $query return $query
->with(['likes.user' => function ($query) { ->with([
$query->where('is_protected', false) 'likes' => function ($query) {
->orWhere('id', Auth::id()); $query->latest()->take(10);
}]) },
'likes.user' => function ($query) {
$query->where('is_protected', false)
->orWhere('id', Auth::id());
}
])
->withCount([ ->withCount([
'likes', 'likes',
'likes as is_liked' => function ($query) { 'likes as is_liked' => function ($query) {
@ -60,9 +66,14 @@ class Ejaculation extends Model
]); ]);
} else { } else {
return $query return $query
->with(['likes.user' => function ($query) { ->with([
$query->where('is_protected', false); 'likes' => function ($query) {
}]) $query->latest()->take(10);
},
'likes.user' => function ($query) {
$query->where('is_protected', false);
}
])
->withCount('likes') ->withCount('likes')
->addSelect(DB::raw('0 as is_liked')); ->addSelect(DB::raw('0 as is_liked'));
} }

View File

@ -3,9 +3,12 @@
namespace App; namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Staudenmeir\EloquentEagerLimit\HasEagerLimit;
class Like extends Model class Like extends Model
{ {
use HasEagerLimit;
protected $fillable = ['user_id', 'ejaculation_id']; protected $fillable = ['user_id', 'ejaculation_id'];
public function user() public function user()