提交表单后重定向后页错误

时间:2019-10-16 12:48:21

标签: php html database laravel

正如标题所述,当我提交表单(报告系统)时,我被重定向回此链接:https://website.com/career_report_event-错误,但是我的数据保存在database中。为什么会这样呢?我应该被重定向回上一页。我添加了sleep(5);来在重定向后5秒钟进行计数,但是现在无法正常工作了。

这是我的代码:

public function careerReportEvent(requ $request)

 {


$reportExists = \App\Reports::where('user_id', $request['user_id'])
         ->whereDate('created_at', '>', Carbon::now()->subMinutes(5)->toDateTimeString())
         ->exists();

         if($reportExists) {
         // report has been created within 5 minutes
         return Redirect::back()->withErrors(['error', 'Report created within the last 5 minutes']);
     }

     $lastReport = \App\Reports::where('user_id', $request['user_id'])
                                ->orderBy('id', 'desc')
                                ->first();


$lastReportOn = time() - \Carbon\Carbon::parse($lastReport->created_at)->timestamp;

    if($lastReportOn < 150 ) {
        // report has been created within 5 minutes
        sleep(5);
        return Redirect::back()->with('error', 'This action was blocked as too many reports were sent in a short amount of time. Please try later.');
    }
     $report = \App\Reports::create([
                     'user_id' => $request['user_id'],
                     'username' => $request['username'],
                     'user_id_posted' => $request['user_id_posted'],
                     'username_posted' => $request['username_posted'],
                     'career_solution_id' =>$request['career_solution_id'],
                     'subject' =>$request['subject'],
                     'why_reporting' =>$request['why_reporting'],
                     'why_reporting_message' =>$request['why_reporting_message'],
                     'additional_message' =>$request['additional_message'],
                     'comment' =>$request['comment'],
                     'comment_user' =>$request['comment_user'],
                     'comment_id' =>$request['comment_id'],
                   ]);
                   $id = $request['career_solution_id']; // looks like this is the ID you ar looking for

                   $event = Event::findOrfail($id);
                   $event->active = $request['active'];
                   $event->save();

                   if ($user = Sentinel::check())
                  {

                        $data = $this->data;
                         $user_id = $user->id;
                     // return $request->all();





                                 $career_solution = \App\CareerSolution::findOrFail($user_id);

                                  $log = \App\Log::create([
                                 'user_id' => $request['user_id_posted'],
                                 'log' => $request->title,
                                 'date' => time(),
                                 'article_type' => 'event',
                                 'type' => 'report',
                                 'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
                                 'link' => $request['subject'],
                                 'action_user_id' => $user_id,
                                 'action_name' => $user->name,
                                 'action_username' => $user->username,
                                 'item_id' => $request['career_solution_id'],
                                 'profile_picture' => $request['profile_picture']

                             ]);
                             $id = $request['career_solution_id'];
                             $news = News::findOrfail($id);

                             $news_user = User::findOrfail($news->user_id);

                          $my_alerts = \App\MyAlert::where('user_id','=',$news->user_id)->first();
                          if(isset($my_alerts))
                          {
                             if(($my_alerts->report == 1) && ($my_alerts->email_frequency == 1))
                             {

                                 $message = "";
                                 $message .= "<table style='width:100%'>";
                                 $message .= "<tr>";
                                 $message .= "<td style='width:40%'>";
                                 $message .= "<img src='".asset('assets/img/WS_logo.png')."' height='35' width='185'>";
                                 $message .= "</td><td>";
                                 $message .= "</td><td style='float:right;'>";
                                 $message .= date('F jS, Y');
                                 $message .= "</td></tr></table><hr><br>";

                                 $message .= "</td></tr><tr align='center'>";
                                 $message .= "<td>If you do not wish to receive this messages, access your Workstickers acoount / Alerts Section and disable it.</td></tr>";
                                 $message .= "<tr align='center'><td><img src='".asset('assets/img/font-image/share_facebook.png')."'>&nbsp;&nbsp;<img src='".asset('assets/img/font-image/skype-icon.png')."'>&nbsp;&nbsp;<img src='".asset('assets/img/font-image/linkedin.png')."'>&nbsp;&nbsp;<img src='".asset('assets/img/font-image/googleplus_icon.png')."'>&nbsp;&nbsp;<img src='".asset('assets/img/font-image/twitter_icon.jpg')."'></td></tr>";


                                 Mail::send('send', ['content' => $message], function ($m) use ($news_user,$user)
                                 {
                                     $m->from('hello@workstickers.com', "Workstickers");
                                     $m->to($news_user->email, $news_user->name)->subject("Workstickers alert : ".$user->username." has reported one of your articles.");
                                 });
                             }

                   }
                  }

     if($report != ""){
       sleep(5);
         flash('Career solution report submited', 'success');
     }else{
         flash('Career solution report', 'warning');
     }



     return Redirect::back();


 }

这是我的路线:

Route::post('career_report_event', 'CareerSolutionController@careerReportEvent');

2 个答案:

答案 0 :(得分:0)

您应该将以前的URL保存在您的表单中,将其发送到您的控制器,并发送到该URL返回的控制器中。

代码形式

<input type="hidden" name="url" value="{{ URL::previous() }}">

代码控制器

return redirect($request->url);

答案 1 :(得分:0)

确保先导入正确的名称空间

use Illuminate\Http\RedirectResponse;

您可以在代码中编写

return redirect()->back();
相关问题