Looping through query results and and comparing them to see if a value exists and pushing them to a collection in laravel/php

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am using Laravels built in notifications system to save notifications to a database. One project can have multiple types of notifications, or no notifications. I am currently querying for all the unread notifications, and also for projects for each user. I loop through both the projects and notifications results and check to see if the project address exists in the notifications results, if it does not that means there are no notifications for that project so I manually create a notification stating that so I can display it on the front end. I feel like this could be done much easier/cleaner but I cant think of how.



$nots = $user->unreadNotifications->all();
$notsCollect = new Collection();
$projects = AppProject::where('userID', $user->id)->groupBy('address')->get();

foreach ($projects as $proj)
foreach ($nots as $not)
if ($proj->address == $not->data['projectAddress'])
$notsCollect->push($not);

else
$data = array('projectAddress' => $proj->address ,'projectAddress2' => $proj->address2);
$values = ([
'type' => 'NoNotifications',
'data' => $data
]);
$notsCollect->push($values);




return response()->json(['nots'=> $notsCollect->groupBy('data.projectAddress')], 200);






share|improve this question



















  • This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
    – Indra
    Mar 31 at 20:26
















up vote
0
down vote

favorite












I am using Laravels built in notifications system to save notifications to a database. One project can have multiple types of notifications, or no notifications. I am currently querying for all the unread notifications, and also for projects for each user. I loop through both the projects and notifications results and check to see if the project address exists in the notifications results, if it does not that means there are no notifications for that project so I manually create a notification stating that so I can display it on the front end. I feel like this could be done much easier/cleaner but I cant think of how.



$nots = $user->unreadNotifications->all();
$notsCollect = new Collection();
$projects = AppProject::where('userID', $user->id)->groupBy('address')->get();

foreach ($projects as $proj)
foreach ($nots as $not)
if ($proj->address == $not->data['projectAddress'])
$notsCollect->push($not);

else
$data = array('projectAddress' => $proj->address ,'projectAddress2' => $proj->address2);
$values = ([
'type' => 'NoNotifications',
'data' => $data
]);
$notsCollect->push($values);




return response()->json(['nots'=> $notsCollect->groupBy('data.projectAddress')], 200);






share|improve this question



















  • This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
    – Indra
    Mar 31 at 20:26












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using Laravels built in notifications system to save notifications to a database. One project can have multiple types of notifications, or no notifications. I am currently querying for all the unread notifications, and also for projects for each user. I loop through both the projects and notifications results and check to see if the project address exists in the notifications results, if it does not that means there are no notifications for that project so I manually create a notification stating that so I can display it on the front end. I feel like this could be done much easier/cleaner but I cant think of how.



$nots = $user->unreadNotifications->all();
$notsCollect = new Collection();
$projects = AppProject::where('userID', $user->id)->groupBy('address')->get();

foreach ($projects as $proj)
foreach ($nots as $not)
if ($proj->address == $not->data['projectAddress'])
$notsCollect->push($not);

else
$data = array('projectAddress' => $proj->address ,'projectAddress2' => $proj->address2);
$values = ([
'type' => 'NoNotifications',
'data' => $data
]);
$notsCollect->push($values);




return response()->json(['nots'=> $notsCollect->groupBy('data.projectAddress')], 200);






share|improve this question











I am using Laravels built in notifications system to save notifications to a database. One project can have multiple types of notifications, or no notifications. I am currently querying for all the unread notifications, and also for projects for each user. I loop through both the projects and notifications results and check to see if the project address exists in the notifications results, if it does not that means there are no notifications for that project so I manually create a notification stating that so I can display it on the front end. I feel like this could be done much easier/cleaner but I cant think of how.



$nots = $user->unreadNotifications->all();
$notsCollect = new Collection();
$projects = AppProject::where('userID', $user->id)->groupBy('address')->get();

foreach ($projects as $proj)
foreach ($nots as $not)
if ($proj->address == $not->data['projectAddress'])
$notsCollect->push($not);

else
$data = array('projectAddress' => $proj->address ,'projectAddress2' => $proj->address2);
$values = ([
'type' => 'NoNotifications',
'data' => $data
]);
$notsCollect->push($values);




return response()->json(['nots'=> $notsCollect->groupBy('data.projectAddress')], 200);








share|improve this question










share|improve this question




share|improve this question









asked Jan 18 at 17:12









Anonguy123

63




63











  • This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
    – Indra
    Mar 31 at 20:26
















  • This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
    – Indra
    Mar 31 at 20:26















This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
– Indra
Mar 31 at 20:26




This is hoe you get server timeout. Use query builder to have a join and try doing only one loop
– Indra
Mar 31 at 20:26















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

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: "196"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185412%2flooping-through-query-results-and-and-comparing-them-to-see-if-a-value-exists-an%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185412%2flooping-through-query-results-and-and-comparing-them-to-see-if-a-value-exists-an%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods