Laravel redirect with errormessagebag not returning the actual message

  laravel, laravel-8, laravel-blade, laravel-response, php

I started a new laravel 8 project and installed the laravel breeze auth starter kit. After that i made an api endpoint and i want to validate the post request going to that endpoint so i wrote some code:


        $validation = Validator::make($request->all(), [
            'project_name' => 'required',
            'project_location' => 'required',
            'project_tasks' => 'required',
        ]);

        if ($validation->fails()) {
            return redirect()->back()->with('project_error', $validation->errors());
        }

But when i print out

{{ dump(Session::get('project_error') }}

its empty, so i solved it by adding the following

IlluminateSessionMiddlewareStartSession::class,

to protected $middleware in App/Http/Kernel.php
That solves the issue but then my breeze starter kit login does not return a response when i enter the wrong details and it logs me out when i make an post request to the project api endpoint.

breeze auth code:

    public function store(LoginRequest $request)
    {
        $request->authenticate();

        $request->session()->regenerate();

        return redirect()->intended(RouteServiceProvider::HOME);
    }

    public function authenticate()
    {
        $this->ensureIsNotRateLimited();

        if (!Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
            RateLimiter::hit($this->throttleKey());

            throw ValidationException::withMessages([
                'email' => __('auth.failed'),
            ]);
        }

        RateLimiter::clear($this->throttleKey());
    }

Does anyone know how i can still have the breeze auth response and the return redirect()->back()->with(‘project_error’, $validation->errors()); at the same time.

I hope i provided enough info 🙂

Thanks in advance!

Source: Laravel

Leave a Reply