如何在不丢失(丢失)Laravel PHP框架中的键的情况下合并两个集合? [英] How to merge two collections without dropping (losing) keys in Laravel PHP framework?
问题描述
我是Laravel/PHP的新手,正在做我的个人玩具项目",遇到了一个我已经使用Google谷歌搜索很长时间的问题.但是,我想不出一个完美的解决方案.
I am new to Laravel/PHP, and I am doing my personal "toy project" and I met an issue that I have been Googling for a long time. However, I cannot figure out a perfect solution.
问题是,我有两个集合$questions
和$answers
,我想将它们合并为一个大集合.这是这两个集合的结构/示例
The problem is that, I have two collections $questions
and $answers
, and I would like to merge them to a big one collection. Here are the structure/example for these two collections
$questions
:
questions: [
{
id: 1,
title: "Why do PHP developers love Laravel? What are the things that distinguish Laravel from other PHP frameworks?",
desc: null,
user_id: 2,
created_at: "2018-07-15 06:45:57",
updated_at: "2018-07-15 06:45:57",
status: "ok"
}
]
$answers
:
answers: [
{
id: 2,
content: "Laravel is usually considered as PHP on rails! It is amazing!",
user_id: 2,
question_id: 1,
created_at: "2018-07-15 07:11:39",
updated_at: "2018-07-15 07:11:39"
},
{
id: 1,
content: "PHP is the best programming language in the world! [smile]",
user_id: 1,
question_id: 1,
created_at: "2018-07-15 07:02:21",
updated_at: "2018-07-15 07:05:12"
}
]
我尝试了merge
命令,并且得到了如下内容:
I have tried merge
command and I got something like this:
$data = $questions->merge($answers);
$data = $data->sortByDesc(function($item) {
return $item->created_at;
});
data: [
{
id: 2,
content: "Laravel is usually considered as PHP on rails! It is amazing!",
user_id: 2,
question_id: 1,
created_at: "2018-07-15 07:11:39",
updated_at: "2018-07-15 07:11:39"
},
{
id: 1,
content: "PHP is the best programming language in the world! [smile]",
user_id: 1,
question_id: 1,
created_at: "2018-07-15 07:02:21",
updated_at: "2018-07-15 07:05:12"
}
]
注意到,在$data
中我再也找不到了问题,但是我确实想在$data
集合中解决该问题.此外,我尝试了union
,push
,put
之类的命令,但没有一个能提供令人满意的解决方案.
Noticed that, in $data
, I cannot find the question anymore, but I do wanna it in the $data
collection. Besides, I have tried commands like union
, push
, put
, none of them gives a satisfying solution.
我的代码是否存在问题,或者我根本无法在PHP中做到这一点?如果我的代码中有错误,请粘贴到这里.
Is there something wrong with my code or I cannot do this in PHP at all? In case there are bugs in my codes, I have pasted here.
public function timeline()
{
list($limit, $skip) = paginate(rq('page'), rq('limit'));
/* Retrieve questions, $questions is like I pasted above*/
$questions = question_init()
->limit($limit)
->skip($skip)
->orderBy('created_at', 'desc')
->get();
/* Retrieve answers, $answers is like I pasted above */
$answers = answer_init()
->limit($limit)
->skip($skip)
->orderBy('created_at', 'desc')
->get();
/* Merge questions and answers */
$data = $questions->merge($answers);
/* Sort by Created time */
$data = $data->sortByDesc(function($item) {
return $item->created_at;
});
$data = $data->values()->all();
return succ(['questions' => $questions, 'answers' => $answers, 'data' => $data]);
}
推荐答案
这是因为扩展支持集合的Eloquent集合使用的字典由合并时所包含的Model实例的关键字作为关键字.它们具有专门用于处理模型集合的特殊功能.通常,因此您不会像这样合并Eloquent Collections.
This is because Eloquent Collections which extend Support Collections use a dictionary that is keyed by the key of the Model instances contained when merging. They have special functionality specifically for working with a collection of models. Usually you don't merge Eloquent Collections like this because of that.
您可以从其中一个雄辩的收藏中获取支持收藏,然后将另一个收藏合并到其中.
You can get a Support Collection from one of the Eloquent Collections and merge the other collection into it.
$collection->toBase()
会给您Illuminate\Database\Eloquent\Collection
中的Illuminate\Support\Collection
.
您可以尝试:
$data = $questions->toBase()->merge($answers);
这篇关于如何在不丢失(丢失)Laravel PHP框架中的键的情况下合并两个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!