1

Check if the request record exists in the method show() .If they do not exist call the method DislayNotFound.

Have any of you tried this method? Can you help me?

This is my file BaseController:

<?php

namespace App\Http\Controllers;

class BaseController extends Controller
{
    public function display_not_found ()
    {
        return response(['message' => 'Not found'], 404);
    }
}

This is my file FontController:

<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseCollection;
use App\Http\Resources\FontResource;
use App\Models\Design;
use App\Models\Font;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Enums\CursorPaginate;
use App\Enums\ResponseMessages;
use App\Http\Resources\DesignResource;
use Illuminate\Support\Facades\Redis;

class FontController extends BaseController
{
 public function show(Font $font)
    {
        if(!$font->exists()){
            return $this->display_not_found();
        }

        if($font['user_id'] !== auth()->user()->id) {
            return response(['message' => 'Forbidden'], 403);
        }

        return new FontResource($font);
    }
}

Error:

enter image description here

5
  • What isn't working? What do you need help with?
    – Rwd
    Sep 20, 2021 at 8:54
  • As in, it isn't getting called, or it's throwing an error, or it's returning something different, or the if statement is triggering when it shouldn't or it isn't triggering when it should...or something else entirely? Just an FYI, Laravel handles throwing a 404 when a model doesn't exist automatically when you use route model binding (which it looks like you are)?
    – Rwd
    Sep 20, 2021 at 9:04
  • I'm getting an error checking if the required record exists. But my message display comes out like the image above and not as expected is 'message' => 'Not found' in BaseController. Sep 20, 2021 at 9:08
  • I imagine this is because of route model binding i.e. if Laravel can't find the model, it will return a 404 for you with a bit more detail. As you can see from your screenshot, it is returning a 404 status. When you have APP_DEBUG set to false, it will return less information. What version of Laravel are you using? Are you wanting to change the 404 message for all models or just this one?
    – Rwd
    Sep 20, 2021 at 9:17
  • I am using laravel version 8. Currently i want to change the 404 message for this model only. Is there any way you can help me with this problem? Sep 20, 2021 at 9:36

1 Answer 1

3

If you would like to change the error response, you can override the Exception Hander. To do this, add the following to the register method in your your app/Exceptions/Handler.php file:

$this->renderable(function (NotFoundHttpException $e, $request) {
    $previous = $e->getPrevious();

    if (
        $previous instanceof ModelNotFoundException &&
        $previous->getModel() === Font::class &&
        $request->expectsJson()
    ) {
        return response()->json(['message' => 'Not found'], 404);
    }
});

Don't forget to import the necessary classes are the top:

use App\Models\Font;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

Alternatively, you could remove route model binding for this method (the easiest way to do this would be to remove the Font type hint) and manually try to find the model.

3
  • I tried your way and it worked according to my wish. Thank you very much. Sep 20, 2021 at 10:07
  • Sorry but can I bother you a little more. If I want to change the 404 message for all models, will I have to change it in Font::class or something like that? Can you suggest me? Sep 20, 2021 at 10:23
  • @CôngNguyễn If you want to change it for all models, then you can remove the line $previous->getModel() === Font::class && from the Handler class. If the above has answered your question, please may you mark the answer as accepted :).
    – Rwd
    Sep 20, 2021 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.