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

Clash 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);
php laravel
add a comment |Â
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);
php laravel
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
add a comment |Â
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);
php laravel
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);
php laravel
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
add a comment |Â
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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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