Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何处理Laravel 5中的PDO异常_Php_Laravel_Pdo_Eloquent_Laravel 5.1 - Fatal编程技术网

Php 如何处理Laravel 5中的PDO异常

Php 如何处理Laravel 5中的PDO异常,php,laravel,pdo,eloquent,laravel-5.1,Php,Laravel,Pdo,Eloquent,Laravel 5.1,我已将Exceptions/Handler.php修改为: <?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Symfony\Comp

我已将Exceptions/Handler.php修改为:

    <?php namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler {

    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
    'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {

        if($e instanceof NotFoundHttpException)
        {
            return response()->view('errors/404');
        }

        elseif ($e instanceof ErrorException) {
            return response()->view('errors/404');
        }

        elseif ($e instanceof ModelNotFoundException) {
            return response()->view('error_log(message)ors/404');
        }

        elseif($e instanceof PDOException)
        {
            return Redirect::to('install.php');
        }

        elseif($e instanceof QueryException)
        {
            return Redirect::to('install.php');
        }

        else return response()->view('errors/error');


    }

我想我已经找到了解决方案:

use PDOException; 
包括 使用异常

public function render($request, Exception $e)
    {

        if($e instanceof PDOException)
        {
            return response()->view('errors/404');
        }
在异常/处理程序中
它对我有效

这就是在Laravel 5.3中对我有效的内容(摘自
/app/Exceptions/
中我的
Handler.php
文件):

或者尝试{}catch(\pdotexception$pde){}
public function report(Exception $e)
{

    // Both this, and below, are needed
    if($e instanceof \PDOException) {
        return response()->view('errors/404');
    }

    return parent::report($e);
}

public function render($request, Exception $e)
{

    if($e instanceof \PDOException) {
        // TODO send me an email!
        Session::flash('messageclass', 'danger');
        Session::flash('message', trans('general.pdoexception'));
        return redirect()->back();
    }

    return parent::render($request, $e);
}