반응형
컬렉션에서 요소의 인덱스를 검색하는 방법은 무엇입니까? 내 코드 :
$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();
$userIndex = $users->search(function($user) {
return $user->id === Auth::id();
});
인덱스가 0 일 수 있으므로주의하십시오.
// 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
}
참조 페이지 https://stackoverflow.com/questions/53661967
반응형
'MySql' 카테고리의 다른 글
MySQL max () + 1 문제로 값 삽입 및 설정 (0) | 2020.10.14 |
---|---|
MySQL 테이블에서 두 번째로 높은 값 가져 오기 (0) | 2020.10.14 |
MySQL 데이터베이스 레코드를 배열에 저장 (0) | 2020.10.14 |
MySQL null 인 mysql update increment int 필드 (0) | 2020.10.14 |
MySQL 조건이있는 경우 MySQL 트리거 (0) | 2020.10.14 |
댓글