Api: ModelNotFoundException

Created on 3 Jan 2016  路  4Comments  路  Source: dingo/api

When I write User::findOrFail(100), and id=100 does not exisit. It will throw ModelNotFoundException, message is No query results for model [...]. I want to return code and message using dingp/api in this situation. How to do that?

I tried to add if ($e instanceof ModelNotFoundException) {...} in app/Exceptions/Handle.php render method, it does not work.

Most helpful comment

I add this to AppServiceProvider.

$this->app->make('api.exception')->register(function (ModelNotFoundException $e) {
    return abort(404);
});

All 4 comments

I add this to AppServiceProvider.

$this->app->make('api.exception')->register(function (ModelNotFoundException $e) {
    return abort(404);
});

@crynobone In AppServiceProvider register method? It tells me api.exception class does not exist

Boot dingo.api before app service provider.

$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
// ...
$app->register(App\Lumen\Providers\AppServiceProvider::class);

And do it in boot, not register.

Was this page helpful?
0 / 5 - 0 ratings