从另一个集合创建一个新集合

时间:2017-02-20 13:42:48

标签: php laravel laravel-5 eloquent

我正在从我创建的查询中创建一个特定数据的集合,但是我需要创建一个只有一些带有自定义名称属性的数据的新集合,我正在使用数组,但我需要在集合中创建它,因为它是easyer格式化数据并访问一些集合方法。

我目前的代码如下:

    $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订购集合。

2 个答案:

答案 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_atupdated_at默认包含在此作为BaseModel的一部分,我认为我可能错了。