Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔不同对象分组_Php_Mysql_Laravel - Fatal编程技术网

Php 拉威尔不同对象分组

Php 拉威尔不同对象分组,php,mysql,laravel,Php,Mysql,Laravel,如何将两个不同的对象合并为一个?我想在一个时间轴上显示两个表,我需要按创建日期对它们进行分组。如果我尝试只显示一个表,它可以正常工作,但我希望按日期将它们合并。因此,如果在不同的表中有两行具有相同的创建日期,我希望它们按该日期分组 $comments = array_slice(WPComment::with('recipe')->orderBy('comment_date', 'desc')->get()->groupBy('comment_date'

如何将两个不同的对象合并为一个?我想在一个时间轴上显示两个表,我需要按创建日期对它们进行分组。如果我尝试只显示一个表,它可以正常工作,但我希望按日期将它们合并。因此,如果在不同的表中有两行具有相同的创建日期,我希望它们按该日期分组

    $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)
,但这显然不能完成元素的完全合并,因为已经完成了一些重写。将数组与
array\u merge()
合并基本相同。有没有一种单行解决方案,而不需要写一些令人讨厌的raw's或double for loop胡言乱语

编辑

之前的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后的示例:

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
             }

        }

我已经设法用
array\u merge\u recursive()
方法解决了我的问题

现在,完成解决方案:

$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);

我已经设法用
array\u merge\u recursive()
方法解决了我的问题

现在,完成解决方案:

$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);

一个合并项目的外观如何?顶部JSON数字将是日期,然后是每种类型的对象,这取决于它们的日期是否匹配示例:合并前和合并后的对象。一个合并项目的外观如何?顶部JSON数字将是日期,然后是每种类型的对象,取决于它们的日期是否匹配示例:合并前后的对象。