我目前的代码如下:
$activity = [];
$temp = [];
$calculations = collect($user->calculations()
->withTrashed()
->orderBy('updated_at', 'desc')
->get());
foreach($calculations as $calculation){
$temp['type'] = "calculation";
$temp['name'] = $calculation->name;
$user = $this->getUserById($calculation->pivot->user_id);
$temp['user'] = $user->name ." ".$user->surname;
if($calculation->created_at == $calculation->updated_at && $calculation->deleted_at == null)
{
$temp['operation'] = "saved";
$temp['date'] = $calculation->created_at;
$temp['diff'] = Carbon::parse($calculation->created_at)->diffForHumans();
}elseif($calculation->created_at != $calculation->updated_at && $calculation->deleted_at != null)
{
$temp['operation'] = "changed";
$temp['date'] = $calculation->updated_at;
$temp['diff'] = Carbon::parse($calculation->updated_at)->diffForHumans();
}else{
$temp['operation'] = "delete";
$temp['date'] = $calculation->deleted_at;
$temp['diff'] = Carbon::parse($calculation->deleted_at)->diffForHumans();
}
array_push($activity,$temp);
}
$conditions = collect($user->conditions()
->withTrashed()
->orderBy('updated_at', 'desc')
->get());
foreach($conditions as $condition){
$temp['type'] = "condition";
$temp['name'] = $condition->name;
$user = $this->getUserById($condition->user_id);
$temp['user'] = $user->name ." ".$user->surname;
if($condition->created_at == $condition->updated_at && $condition->deleted_at == null)
{
$temp['operation'] = "saved";
$temp['date'] = $condition->created_at;
$temp['diff'] = Carbon::parse($condition->created_at)->diffForHumans();
}elseif($condition->created_at != $condition->updated_at && $condition->deleted_at != null)
{
$temp['operation'] = "alterado";
$temp['date'] = $condition->updated_at;
$temp['diff'] = Carbon::parse($condition->updated_at)->diffForHumans();
}else{
$temp['operation'] = "delete it";
$temp['date'] = $condition->deleted_at;
$temp['diff'] = Carbon::parse($condition->deleted_at)->diffForHumans();
}
array_push($activity,$temp);
我已经将eloquent查询转换为“collect”,但是如何创建新的集合,我需要使用数组方法,我应该使用集合方法来创建它们。
基本上我的主要原因是我需要合并“条件”和“计算”,而不是能够为dataTime订购集合。
答案 0 :(得分:0)
这样的事情怎么样。
我在集合上使用了transform
方法(为了转换键名)。我已经复制了你的逻辑,然后合并了两个集合。
$calculations = $user->calculations()
->withTrashed()
->orderBy('updated_at', 'desc')
->get();
$transformed = $calculations->transform(function($item, $key) use($user) {
$toReturn = [];
$toReturn['type'] = "calculation";
$toReturn['name'] = $item->name;
$toReturn['user'] = $user->name;
if($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif($item->created_at != $item->updated_at && $item->deleted_at != null){
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn;
});
$conditions = $user->conditions()
->withTrashed()
->orderBy('updated_at', 'desc')
->get();
$transformed2 = $conditions->transform(function($item, $key) use($user) {
$toReturn = [];
$toReturn['type'] = "calculation";
$toReturn['name'] = $item->name;
$toReturn['user'] = $this->getUserById($item->user_id);
if($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif($condition->created_at != $condition->updated_at && $condition->deleted_at != null){
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn
});
$merged = $transform->merge($transform2);
答案 1 :(得分:0)
基于@devk的答案,这是一个更简洁的版本,没有那么多重复的代码:
/**
* Transform a collction with a given callback
*
* @param Collection $collection A laravel collection
* @param User $user A User object
* @return Collection
**/
private function transformCollection(Collect $collection, User $user) {
return $collection->transform(function($item, $key) use ($user) {
$toReturn = [
'type' => 'calculation',
'name' => $item->name,
'user' => $user->name
];
if ($item->created_at == $item->updated_at && $item->deleted_at == null) {
$toReturn['operation'] = "saved";
$toReturn['date'] = $item->created_at;
$toReturn['diff'] = Carbon::parse($item->created_at)->diffForHumans();
} elseif ($item->created_at != $item->updated_at && $item->deleted_at != null) {
$toReturn['operation'] = "changed";
$toReturn['date'] = $item->updated_at;
$toReturn['diff'] = Carbon::parse($item->updated_at)->diffForHumans();
} else {
$toReturn['operation'] = "delete";
$toReturn['date'] = $item->deleted_at;
$toReturn['diff'] = Carbon::parse($item->deleted_at)->diffForHumans();
}
return $toReturn;
});
}
// Return all user calculations ordered by when they were updated including deleted
$calculations = $user->calculations()->withTrashed()->orderBy('updated_at', 'desc')->get();
$conditions = $user->conditions()->withTrashed()->orderBy('updated_at', 'desc')->get();
// Transform both collections
$transformed = transformCollection($calculations, $user);
$transformed2 = transformCollection($conditions, $user);
// Merge the resulting collections into a single collection
$merged = $transform->merge($transform2);
如果您的Calculation
对象有模型,您还可以通过将日期添加到Carbon
数组
protected $dates = []
日期返回
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
我认为created_at
和updated_at
默认包含在此作为BaseModel
的一部分,我认为我可能错了。