mohammad nabipour
mohammad nabipour

Reputation: 77

how to handle http request error in blade?

I am getting an HTTP request in my controller like this:

try {
    $cards = $client = Http::acceptJson()->get('www.my.local', $request->input());
    $response = $client->json();

    if ($client->clientError()) {
        throw new HttpException(400, $error['errors'][0]['detail'] ?? 'Server busy');
    }

    return view('card::cards.index')
        ->with([
            'items' => $cards['data'],
            'meta' => $cards['meta'],
        ]);
} catch (\Exception $exception) {
    return back()->withErrors($exception->getMessage())->withInput();
}

The error response:

[ 
   "error" => "validate error",
   "fields" => [
      "id" => "init",
      ......
   ]
]

And somethings like this:

[ 
    "error" => "server error"
]

I want to show all of the errors array in my blade but I can't.

Upvotes: 0

Views: 817

Answers (1)

SEYED BABAK ASHRAFI
SEYED BABAK ASHRAFI

Reputation: 4271

You can get errors in blade file and showing them using $errors variable which is available in your blade. More info in laravel doc

@if($errors->any())
    <ul>
    @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@endif

Upvotes: 1

Related Questions