Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amir5's avatar
Level 7

How to use $errors in laravel blade component

I want to get validation errors inside of blade component(php file), how to do that? in typical blade pages I could use @error, and get the message if there is corresponding validation error.

1 like
8 replies
amir5's avatar
Level 7

@vincent15000 @error only works in blade files, not php component(in the php file)

1 like
vincent15000's avatar

@amir5 Blade components are blade files. You just have to pass the error to the component.

Shivamyadav's avatar

@amir5 you can pass the error field name with props to your component. Let you have a input component File

@props(['name', 'errors'])

<div>
    <label for="{{ $name }}">{{ ucfirst($name) }}</label>
    <input id="{{ $name }}" type="text" name="{{ $name }}" value="{{ old($name) }}">

    <!-- Display validation error if it exists -->
    @if ($errors->has($name))
        <p class="text-red-600 text-sm">{{ $errors->first($name) }}</p>
    @endif
</div>

Here how to use it

<x-form.input name="email" :errors="$errors" />

amir5's avatar
Level 7

@Shivamyadav when you create a component e.g, make:component Test, it creates two files, one app/View/Components/Test.php, and resources/views/components/test.blade.php. I want to get the errors inside of app/View/Components/Test.php file, not resources/views/components/test.blade.php blade file

1 like
tykus's avatar
tykus
Best Answer
Level 104

@amir5 you can get the ErrorBag inside a component which will return a MessageBag instance

$errors = $this->getErrorBag() // MessageBag

Then you can interrogate it in a similar way to the Blade templates

$errors->has('email') // Boolean; does the bag have any `email` messages
$errors->first('email') // first error message for `email` field
$errors->any() // Boolean; does the bag have any messages?
$errors->all() // array of all messages in the bag
2 likes

Please or to participate in this conversation.