lundi 20 février 2017

Creating a new collection from another collection

im creating a collection of specific data from a query that i made, but i need to create a new collection with only some data with custom names properties, i was using arrays, but i need to make it in collections since is easyer to format the data and access some collections methods.

My current code is like:

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

I already convert the eloquent query to "collect", but how i cant createa new collections, i need to instead using the array methods, i should use the collection methods to create them.

Basically my main reason is that i need to merge the "conditions" and "calculations" for than be able to order the dataTime the collections.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lyVZVx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire