How can I group multiple columns into an object when making a database call in Laravel?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Let's say I am fetching users



Each user has 10 columns associated with it



How can I group 5 columns together into an object on each user so I can more easily map over them with javascript?



I could do



foreach ($users as $user)
{
$user['new_object']['col_1'] = $user->col_1;
unset($user->col_1);
$user['new_object']['col_2'] = $user->col_2;
// etc
}


But surely there is a way to group columns into an object on Laravel?



For example, something like



$users = User::where('verified', 1)
->group('new_object', ['col_1', 'col_2', 'col_3']),
->get();


Or is there a way for me to update my User model to do this?










share|improve this question

























  • Use collections to manipulate data within the PHP. Good Luck!

    – Kyslik
    Nov 24 '18 at 21:43


















0















Let's say I am fetching users



Each user has 10 columns associated with it



How can I group 5 columns together into an object on each user so I can more easily map over them with javascript?



I could do



foreach ($users as $user)
{
$user['new_object']['col_1'] = $user->col_1;
unset($user->col_1);
$user['new_object']['col_2'] = $user->col_2;
// etc
}


But surely there is a way to group columns into an object on Laravel?



For example, something like



$users = User::where('verified', 1)
->group('new_object', ['col_1', 'col_2', 'col_3']),
->get();


Or is there a way for me to update my User model to do this?










share|improve this question

























  • Use collections to manipulate data within the PHP. Good Luck!

    – Kyslik
    Nov 24 '18 at 21:43














0












0








0








Let's say I am fetching users



Each user has 10 columns associated with it



How can I group 5 columns together into an object on each user so I can more easily map over them with javascript?



I could do



foreach ($users as $user)
{
$user['new_object']['col_1'] = $user->col_1;
unset($user->col_1);
$user['new_object']['col_2'] = $user->col_2;
// etc
}


But surely there is a way to group columns into an object on Laravel?



For example, something like



$users = User::where('verified', 1)
->group('new_object', ['col_1', 'col_2', 'col_3']),
->get();


Or is there a way for me to update my User model to do this?










share|improve this question
















Let's say I am fetching users



Each user has 10 columns associated with it



How can I group 5 columns together into an object on each user so I can more easily map over them with javascript?



I could do



foreach ($users as $user)
{
$user['new_object']['col_1'] = $user->col_1;
unset($user->col_1);
$user['new_object']['col_2'] = $user->col_2;
// etc
}


But surely there is a way to group columns into an object on Laravel?



For example, something like



$users = User::where('verified', 1)
->group('new_object', ['col_1', 'col_2', 'col_3']),
->get();


Or is there a way for me to update my User model to do this?







php mysql laravel eloquent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 23:14







Dazzle

















asked Nov 24 '18 at 21:32









DazzleDazzle

6792728




6792728













  • Use collections to manipulate data within the PHP. Good Luck!

    – Kyslik
    Nov 24 '18 at 21:43



















  • Use collections to manipulate data within the PHP. Good Luck!

    – Kyslik
    Nov 24 '18 at 21:43

















Use collections to manipulate data within the PHP. Good Luck!

– Kyslik
Nov 24 '18 at 21:43





Use collections to manipulate data within the PHP. Good Luck!

– Kyslik
Nov 24 '18 at 21:43












2 Answers
2






active

oldest

votes


















1














You can use mutators in your model:



public function getNewObjAttribute() {
return [
$this->col1,
$this->col2,
$this->col3,
];
}


And in $appends attribute inside that model:



protected $appends = [
'new_obj',
];





share|improve this answer
























  • This is the correct answer. Thank you sir!!!

    – Dazzle
    Nov 25 '18 at 13:55






  • 1





    Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

    – Dazzle
    Nov 25 '18 at 15:06



















2














you can do something like this:



$users = User::where('verified', 1)->get()->map(function($row)
{
$row->new_obj = [$row->col1, $row->col2, $row->col3];
unset($row->col1, $row->col2, $row->col3);
return $row;
});





share|improve this answer


























  • That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

    – Dazzle
    Nov 24 '18 at 21:58













  • See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

    – Eriks Klotins
    Nov 24 '18 at 22:04











  • Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

    – Dazzle
    Nov 24 '18 at 22:07














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462550%2fhow-can-i-group-multiple-columns-into-an-object-when-making-a-database-call-in-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use mutators in your model:



public function getNewObjAttribute() {
return [
$this->col1,
$this->col2,
$this->col3,
];
}


And in $appends attribute inside that model:



protected $appends = [
'new_obj',
];





share|improve this answer
























  • This is the correct answer. Thank you sir!!!

    – Dazzle
    Nov 25 '18 at 13:55






  • 1





    Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

    – Dazzle
    Nov 25 '18 at 15:06
















1














You can use mutators in your model:



public function getNewObjAttribute() {
return [
$this->col1,
$this->col2,
$this->col3,
];
}


And in $appends attribute inside that model:



protected $appends = [
'new_obj',
];





share|improve this answer
























  • This is the correct answer. Thank you sir!!!

    – Dazzle
    Nov 25 '18 at 13:55






  • 1





    Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

    – Dazzle
    Nov 25 '18 at 15:06














1












1








1







You can use mutators in your model:



public function getNewObjAttribute() {
return [
$this->col1,
$this->col2,
$this->col3,
];
}


And in $appends attribute inside that model:



protected $appends = [
'new_obj',
];





share|improve this answer













You can use mutators in your model:



public function getNewObjAttribute() {
return [
$this->col1,
$this->col2,
$this->col3,
];
}


And in $appends attribute inside that model:



protected $appends = [
'new_obj',
];






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 8:42









Ali FarhoudiAli Farhoudi

1,08411334




1,08411334













  • This is the correct answer. Thank you sir!!!

    – Dazzle
    Nov 25 '18 at 13:55






  • 1





    Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

    – Dazzle
    Nov 25 '18 at 15:06



















  • This is the correct answer. Thank you sir!!!

    – Dazzle
    Nov 25 '18 at 13:55






  • 1





    Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

    – Dazzle
    Nov 25 '18 at 15:06

















This is the correct answer. Thank you sir!!!

– Dazzle
Nov 25 '18 at 13:55





This is the correct answer. Thank you sir!!!

– Dazzle
Nov 25 '18 at 13:55




1




1





Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

– Dazzle
Nov 25 '18 at 15:06





Also remember to add protected $hidden = ['col_1', 'col_2']; to avoid duplication

– Dazzle
Nov 25 '18 at 15:06













2














you can do something like this:



$users = User::where('verified', 1)->get()->map(function($row)
{
$row->new_obj = [$row->col1, $row->col2, $row->col3];
unset($row->col1, $row->col2, $row->col3);
return $row;
});





share|improve this answer


























  • That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

    – Dazzle
    Nov 24 '18 at 21:58













  • See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

    – Eriks Klotins
    Nov 24 '18 at 22:04











  • Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

    – Dazzle
    Nov 24 '18 at 22:07


















2














you can do something like this:



$users = User::where('verified', 1)->get()->map(function($row)
{
$row->new_obj = [$row->col1, $row->col2, $row->col3];
unset($row->col1, $row->col2, $row->col3);
return $row;
});





share|improve this answer


























  • That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

    – Dazzle
    Nov 24 '18 at 21:58













  • See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

    – Eriks Klotins
    Nov 24 '18 at 22:04











  • Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

    – Dazzle
    Nov 24 '18 at 22:07
















2












2








2







you can do something like this:



$users = User::where('verified', 1)->get()->map(function($row)
{
$row->new_obj = [$row->col1, $row->col2, $row->col3];
unset($row->col1, $row->col2, $row->col3);
return $row;
});





share|improve this answer















you can do something like this:



$users = User::where('verified', 1)->get()->map(function($row)
{
$row->new_obj = [$row->col1, $row->col2, $row->col3];
unset($row->col1, $row->col2, $row->col3);
return $row;
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 22:00

























answered Nov 24 '18 at 21:49









Eriks KlotinsEriks Klotins

1,4381618




1,4381618













  • That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

    – Dazzle
    Nov 24 '18 at 21:58













  • See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

    – Eriks Klotins
    Nov 24 '18 at 22:04











  • Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

    – Dazzle
    Nov 24 '18 at 22:07





















  • That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

    – Dazzle
    Nov 24 '18 at 21:58













  • See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

    – Eriks Klotins
    Nov 24 '18 at 22:04











  • Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

    – Dazzle
    Nov 24 '18 at 22:07



















That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

– Dazzle
Nov 24 '18 at 21:58







That works but I was hoping not to have to map / loop over all of them and build this query on the fly. Also, how to drop the columns that are now grouped?

– Dazzle
Nov 24 '18 at 21:58















See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

– Eriks Klotins
Nov 24 '18 at 22:04





See my updated solution. Looping over the items is that expensive (well as long as you do not have zillions of them)

– Eriks Klotins
Nov 24 '18 at 22:04













Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

– Dazzle
Nov 24 '18 at 22:07







Good call on the unset. Happy to give this the correct answer for now until a refactor comes along ;-) Thank you!

– Dazzle
Nov 24 '18 at 22:07




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462550%2fhow-can-i-group-multiple-columns-into-an-object-when-making-a-database-call-in-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud