如何在laravel中显示异常处理错误消息

如何在laravel中显示异常处理错误消息,laravel,Laravel,我有一个homecontroller,其中我有一个函数storestudent,它应该能够处理异常,如何处理 public function storestudent(Request $request) { try { $student= new student(); $student->sregno= $request['regno']; $student->save();

我有一个homecontroller,其中我有一个函数storestudent,它应该能够处理异常,如何处理

 public function storestudent(Request $request)
    {
      try
      {


        $student= new student();
        $student->sregno= $request['regno'];

            $student->save();
            return redirect('/home');

          } catch (Exception $e){

                throw new Execution;

          }
     } 
try catch异常不起作用,请帮助我修复此问题。 多谢各位

这是Execution.php

<?php

namespace App\Exceptions;

use Exception;

class Execution extends Exception
{

//
}
 public function report(Exception $exception)
{
    if($exception instanceof Execution)
    {
        echo "invalid register no";
    }
    parent::report($exception);
}
它给了我这个错误

This page isn’t working 127.0.0.1 is currently unable to handle this request.
HTTP ERROR 500

任何人都可以帮助我显示我的消息,它应该重定向到同一页面。

在这里,通过“尝试并捕获”,您可以使用日志在记录中保留错误,并可以在应用程序的“日志”部分中检查记录:

public function storestudent(Request $request)
{
  try
  {


    $student= new student();
    $student->sregno= $request['regno'];

        $student->save();
        return redirect('/home');

      } catch(Exception e){
        $error = sprintf('[%s],[%d] ERROR:[%s]', __METHOD__, __LINE__, json_encode($e->getMessage(), true);
        \Log::error($error);
      }

 }
有关日志fascade的更多信息,请查看以下链接:

另一种方法是处理异常自定义异常处理,您可以在这里获得

要在重定向时显示错误消息,只需在laravel中使用flash消息,无需自定义异常。以下代码:

public function storestudent(Request $request)
{
 try
 {


$student= new student();
$student->sregno= $request['regno'];

    $student->save();
    \Session::flash('message','Saved successfully');
    return redirect('/home');// if you want to go to home

  } catch(Exception e){
    \Session::flash('error', 'Unable to process request.Error:'.json_encode($e->getMessage(), true));
  }

   return redirect()->back(); //If you want to go back

 }
快讯:

@if(Session::has('message'))
 <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ 
     Session::get('message') }}</p>
@endif
@if(会话::has('message'))

{ 会话::获取('message')}

@恩迪夫
或对于更常见的flash视图:

 <div class="flash-message">
    @foreach (['danger', 'warning', 'success', 'info','error'] as $msg)
       @if(Session::has($msg))
        <p class="alert alert-{{ $msg }}">{{ Session::get($msg) }} 
        </p>
       @endif
   @endforeach
 </div>

@foreach(['danger'、'warning'、'success'、'info'、'error']作为$msg)
@if(会话::has($msg))

{{{Session::get($msg)}

@恩迪夫 @endforeach
控制器代码:

<?php

  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Candidate;
  use App\student;
  use Exception;

 class Homecontroller extends Controller
 {


//function for login page

    public function insertReg()
    {

        return view('login');

    }

     public function storestudent(Request $request)
    {
     try {
          if ( !student::where('sregno',$request->regno )->exists() ) {
              $student= new student();
              $student->sregno= $request->regno;


            if ($student->save()) {
             \Session::flash('message','Saved successfully');
             return redirect('/home');// if you want to go to home
          } else {
             throw new Exception('Unable to save record.);
          }
        } else {
             throw new Exception('Record alreasy exists with same sregno');
        }
      } catch(Exception $e){
          \Session::flash('error', 'Unable to process request.Error:'.json_encode($e->getMessage(), true));
      }

       return redirect()->back(); //If you want to go back

  }

sir如果条件在此情况下不起作用,如何显示无效输入的错误。它只检查表中是否存在记录。不检查一些随机的错误条目。请帮助我修复它。对于简单的验证,可以使用laravel的Validator。链接:如果你想深入研究,你可以为你的参数验证创建自定义请求。