0

I am trying to develop a local Laravel package where I can put all my blade components and views so I can include my repo in all my future applications. At this point, All things is ok except one problem: "Undefined variable $errors" in my blade. I'm using @error directive. I know the problem occurred in this section of my code but my question is why not included variable $error within the blade view of my package?

Routes:

use Adway\Hauth\Http\Controllers\HauthController;
use Illuminate\Support\Facades\Route;

Route::get('/login',[HauthController::class,'login'])->name('login');
Route::get('/logout',[HauthController::class,'logout'])->name('logout');
Route::get('/register',[HauthController::class,'register'])->name('register');
Route::post('auth/login',[HauthController::class,'loginAutenticate'])->name('hauth.login');
Route::post('auth/register',[HauthController::class,'registerAutenticate'])->name('hauth.register');

Route::middleware('auth')->group(function(){

    Route::get('/dashboard',[HauthController::class,'dashboard'])->name('dashboard');

});

View:

<x-app>

    <div class="p-5 border-2 border-green-500 ">
        <form action="{{ route('hauth.login') }}" method="post">
            @csrf
           <div class="grid grid-cols-2 gap-x-8 gap-y-4">


               <div class="grid grid-cols-2">
                   <label for="name">email:</label>
                   <input type="email" name="email" id="email"  required>
                   @error('email')
                            <span class="bg-red-400">{{ $message }}</span>
                        @enderror

                </div>

                <div class="grid grid-cols-2">
                    <label for="name">password:</label>
                            <input type="password" name="password" id="password" required>
                            @error('password')
                            <span class="bg-red-400">{{ $message }}</span>
                        @enderror
                </div>


                <button type="submit" class="px-2 bg-green-400 max-w-min rounded-md">login</button>

           </div>
        </form>


    </div>
</x-app>

service provider:

 public function boot(): void
    {
        // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'adway');
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'hauth');
        // $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
        $this->loadRoutesFrom(__DIR__.'/../routes/web.php');

        // Publishing is only necessary when using the CLI.
        if ($this->app->runningInConsole()) {
            $this->bootForConsole();
        }
    }
1
  • There is no HauthController and this template does not use {{ $errors }}. Commented Mar 9, 2022 at 0:31

1 Answer 1

1

I has same issue. I can't get validation message any kind of method. I tried Request class, Then in controller $request->validate([]) method, even I tried Validator::make() Class function but I has same issue to get validation message.

but finally I got solution about this. actually Laravel puts validation messages in session.

& then \Illuminate\View\Middleware\ShareErrorsFromSession::class, class bind that errors to views using errors named variable

& then when we can get validation errors messages using $errors->all()

when we set route in default application's route/web.php this process Laravel perform automatically because Out of the box, the web and api middleware groups are automatically applied to your application's corresponding routes/web.php and routes/api.php files by the App\Providers\RouteServiceProvider. see this Laravel doc page

/**
 * The application's route middleware groups.
 *
 * @var array<string, array<int, class-string|string>>
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

-: Solution :-

we need to pass web named middleware group to our route as like below. then \Illuminate\View\Middleware\ShareErrorsFromSession::class will automatically appending Validation messages to views automatically.

Route::middleware('web')->get('test', [TestController::class, 'index']);

or we can create route group & pass middleware to that route group as like below

Route::middleware('web')->group(function(){
    // Route::get('test', [TestController::class, 'index']);
    //... define your routes here
});

I hope this one helps to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.