$users = User::has('posts')->withCount('posts')->orderBy('posts_count')->take(50)->get();
if($users->contains(Auth::id())){
//get the index of auth user id
}
$users = User::has('posts')->withCount('posts')->orderBy('posts_count')->take(50)->get();
// this return a collection. So can do an if like this: $userIndex->count() > 0
$userIndex = $users->filter(function($user) {
return $user->id === Auth::id()
});
// DON'T do this
if($userIndex) {
// this will get skipped if the user is the first one in the collection
}
// Do this instead
if($userIndex !== false) {
// this will work
}
2条答案
按热度按时间doinxwow1#
nukf8bse2#
你可以用这个收藏
search()
方法:https://laravel.com/docs/5.7/collections#method-搜索小心点,因为索引可能是0: