我在哪里返回通知消息,或者如何从代码中返回通知消息?

时间:2019-04-23 03:36:19

标签: laravel laravel-5 message

这个问题是关于何时升级用户以及何时升级用户的通知消息。

控制器:

  public function settleMatches(Request $request) {
    $validator = \Validator::make($request->all(), [
                'match_id' => 'required',
                'team_winner' => 'required'
    ]);

    if ($validator->passes()) {
        $match = \App\Match::find($request->match_id);
        if ($match->status != 'settled') {
            if (!in_array($request->team_winner, ['draw', 'cancelled']) && $match->type == 'main' && $match->subMatches()->activeMatches()->count())
                return ['error' => 'There are still active sub matches, Please settle them first!'];
            else {
                $total_circulating_credits = calculateCredits('total_circulating_credits');
                $match->team_winner = in_array($request->team_winner, ['draw', 'cancelled']) ? null : $request->team_winner;
                $match->status = in_array($request->team_winner, ['draw', 'cancelled']) ? $request->team_winner : 'settled';
                $match->save();
                foreach ($match->bets as $bet) {
                    $user = $bet->user;
                    if (in_array($request->team_winner, ['draw', 'cancelled'])) {
                        $user->credits += $bet->amount;
                        $user->level += 0;
                        $user->curr_exp += 0;
                        $user->prev_exp += 0;
                        $user->exp_needed += 0;
                        $user->reward_token += 0;
                        $bet->gains = 0;
                        $bet->exp_gained += 0;
                        $bet->save();
                        $user->save();
                    } else {
                        if ($bet->team_id == $match->team_winner) {
                            $gains = ($bet->amount * $bet->ratio);
                            $gainexp = $bet->amount;
                            $user->credits += $gains;
                            $user->curr_exp += $gainexp;
                            $bet->exp_gained += $gainexp;
                            $bet->gains = ($gains - $bet->amount);
                            $bet->save();
                            $user->save();
                        } else {
                            $gainexp = $bet->amount;
                            $user->curr_exp += $gainexp;
                            $bet->exp_gained += $gainexp;
                            $bet->gains = -($bet->amount);
                            $user->save();
                            $bet->save();
                        }
                        while($user->curr_exp >= $user->exp_needed){
                             if($user->level == 10 || $user->level == 15 || $user->level == 20)
                               {
                                  $user->reward_token += 1;
                               }
                                  $user->curr_exp -= $user->exp_needed;   
                                  $user->prev_exp = $user->exp_needed;
                               if($user->level <= 19){
                                   $user->exp_needed = $user->prev_exp * 1.4;
                               }
                               elseif($user->level >= 20 && $user->level <= 39){
                                   $user->exp_needed = $user->prev_exp * 1.1;
                               }
                               elseif($user->level >= 40 && $user->level <= 59){
                                   $user->exp_needed = $user->prev_exp * 1.05;
                               }
                               elseif($user->level >= 60 && $user->level <= 79){
                                   $user->exp_needed = $user->prev_exp * 1.04;
                               }
                               elseif($user->level >= 80 && $user->level <= 99){
                                   $user->exp_needed = $user->prev_exp * 1.03;
                               }
                               elseif($user->level == 100){
                                   $user->exp_needed = $user->exp_needed * 0;
                               }
                                   $user->level += 1;
                                   $user->save();
                               }
                        }
                }

                if ($request->team_winner == 'cancelled')
                    $this->cancelChildMatches($match);

                $this->saveMatchReport($match, $total_circulating_credits);
                return ['success' => 'done'];
            }
        } else
            return ['error' => 'Match was already settled, please refresh page!'];
    } else
        return ['error' => $validator->errors()];
}

我只是对如何在每次升级用户时在用户的个人资料上显示实时通知感到困惑。任何想法如何工作?我正在使用laravel framework 5.4.36版本。

1 个答案:

答案 0 :(得分:0)

任何用户升级后,只需在返回查看之前放置此行

Session::flash("key", "Message you want to display");
return view('viewName');

要使代码正常工作,您需要在顶部使用

  

使用会话;

每当您闪烁任何内容时,您都可以在下一个视图中看到这样的内容

@if(Session::has('key'))
    {{ Session::get('key') }}
@endif
  

或者如果您不使用视图并使用API​​进行所有操作,则

您可以返回任何['key'=>'value']对,当您在响应中获得该键时,显示该消息