Display an error message notification using Toastr and the Exception class render() method in Laravel

171 Views Asked by At

Trying to have a dynamic way to catch thrown exceptions and display the custom error message to the user.(Custom meaning each exception has its own message relating to why the exception was thrown.) Here is what I have been toying with so far but have not had much lack.

In a controller/service throw the exception:

throw AppException::withMessages(['message' => 'Quantity should be less than or equal to Stock Available']);

under App/Exceptions I have the AppException class with the render method:

public function render(){
    return response()->view('toastr', ['message'=>$this->message], 500);
}

Under Views I have the toastr view:

<script>
    toastr.error("{{$message}}")
</script>

I have included the toastr view in the main layout below all the scripts and style sheets: (I understand there should probably be an if statement to check if the $message is set, so it only pops up when there is data in message.)

@include('toastr')

What I am missing about how the response in the render method of the exception works or how would you use this structure to do a dynamic exception handling pop up displaying Laravel workflow?

0

There are 0 best solutions below