2016-09-01 4 views
1

어떻게 두 개의 서로 다른 객체를 하나로 병합 할 수 있습니까? 타임 라인에 2 개의 테이블을 표시하고 싶습니다. 생성 날짜별로 그룹화해야합니다. 하나의 테이블 만 표시하려고하면 잘 작동하지만 둘 다 날짜별로 결합하고 싶습니다. 그래서 같은 테이블에 같은 날짜의 행이 두 개있는 경우 그 날짜별로 그룹화하고 싶습니다.Laravel 다른 객체 groupBy

$comments = array_slice(WPComment::with('recipe')->orderBy('comment_date', 
     'desc')->get()->groupBy('comment_date')->toArray(), 0, 5); 
    $favorites = array_slice(WPFavorite::with('user')->orderBy('date_time', 
     'desc')->get()->groupBy('date_time')->toArray(), 0, 5); 

나는 $object1->merge($object2)로 시도했지만 수행 약간의 재 작성이있는 한 그 obvieusly 요소의 완전한 병합을하지 않습니다. 배열을 array_merge()으로 병합하는 것은 기본적으로 똑같습니다. 루프를 횡설수설하기 위해 원시 코드 나 이중 코드를 쓸 필요가없는 한 줄짜리 솔루션이 있습니까?

편집 :

JSON의 예를 이전 한 후

obj1:{ 
    id: 1 
    title:'blah', 
    date: 1.1.2001 
    } 
obj1:{ 
    id: 2 
    title:'blah blah', 
    date: 31.4.2301 
    } 

obj2:{ 
    id: 1 
    notTitle:'blah', 
    somethingSomething : 'something' 
    date: 31.4.2301 
    } 

JSON 예 : 나는 array_merge_recursive() 방법으로 내 문제를 해결하기 위해 관리해야

1.1.2001:{ 
     obj1:{ 
      id: 1 
      title:'blah', 
      date: 1.1.2001 
      } 
     } 
31.4.2301:{ 
     obj1:{ 
      id: 2 
      title:'blah blah', 
      date: 31.4.2301 
      } 
     obj2:{ 
      id: 1 
      notTitle:'blah', 
      somethingSomething : 'something' 
      date: 31.4.2301 
      } 

     } 
+0

어떻게이 같은 항목을 볼 병합 된 것인가? –

+0

맨 위로 JSON 수치가 날짜가되고 그 다음에 해당 날짜가 일치하는지 여부에 따라 아래 유형의 오브젝트가있을 수 있습니다. – Norgul

+0

예제 게시 : 병합 전후의 오브젝트. –

답변

0

.

이제 완벽한 솔루션 :

$comments = array_slice(WPComment::with('recipe')->orderBy('comment_date', 
      'desc')->get()->groupBy('comment_date')->toArray(), 0, 5); 
$favorites = array_slice(WPFavorite::with('user')->orderBy('date_time', 
      'desc')->get()->groupBy('date_time')->toArray(), 0, 5); 

return array_merge_recursive($comments, $favorites);