public function calculate_alternatives_total($user_id) { $total = 0; $user = User::find($user_id); for ($i = 0; $i < count($user->alternatives); $i++) { $user->alternatives[$i]->details = Alternative::find($user->alternatives[$i]->id)->flags()->first(); } foreach ($user->alternatives as $alternative) { $keyword_total = $this->get_keyword_sum($alternative->details->keywords->first()->id); $total += $keyword_total * ($alternative->details->pivot->savings_percent / 100); } return $total; }
/** * Run the database seeds. * * @return void */ public function run() { $flags = array('Starbucks' => array('alternatives' => array(array('title' => 'Tim Hortons', 'data' => array('savings_percent' => 40, 'message' => 'Tim Horton\'s costs much less than Starbucks.')), array('title' => 'Make Coffee At Home', 'data' => array('savings_percent' => 60, 'message' => 'Save a ton of money by making your own coffee.'))), 'keywords' => array('starbucks')), 'Loblaws' => array('alternatives' => array(array('title' => 'Metro', 'data' => array('savings_percent' => 30, 'message' => 'Big savings by shopping at Metro vs other stores.'))), 'keywords' => array('sobeys', 'loblaw', 'zehrs')), 'Casino' => array('alternatives' => array(array('title' => 'Don\'t Gamble', 'data' => array('savings_percent' => 100, 'message' => 'Instead of losing money by gambling, we can instantly use that to cover debts.'))), 'keywords' => array('casino'))); foreach ($flags as $key => $flag) { $new_flag = Flag::create(['title' => $key]); foreach ($flag['alternatives'] as $alternative) { $new_alternative = Alternative::create(['title' => $alternative['title']]); $new_flag->alternatives()->attach($new_alternative->id, ['savings_percent' => $alternative['data']['savings_percent'], 'message' => $alternative['data']['message']]); } foreach ($flag['keywords'] as $keyword) { $new_flag->keywords()->attach(Keyword::where('name', $keyword)->first()->id); } } }
public function user_alternatives($user_id) { if ($user = User::find($user_id)) { for ($i = 0; $i < count($user->alternatives); $i++) { $user->alternatives[$i]->details = Alternative::find($user->alternatives[$i]->id)->flags()->first(); } return response()->json(['response' => 'success', 'alternatives' => $user->alternatives]); } else { return response()->json(['response' => 'success', 'alternatives' => false, 'message' => 'User not found...']); } }