laravellaravel-7laravel-response

Sending flash message via Response Function in Laravel 7


I have a question. I created a middleware to check if the user has an image. Otherwise he goes to the image-upload page and he has to upload an image. Unfortunately in my middleware Redirect Function does not work (I don't know why!) instead I have to use Response Function. How can I send a flash message with a Response Function like Redirect Function? Hopefully you can help me with it.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckUserDataMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Checking through relations if the User has already an image
        if (empty(auth()->user()->profile->image)) {
            return response()
                ->view('pages.image.create', [], 200)
                ->header('Content-Type', $type = '');

            // return redirect()
            //     ->route('image.create')
            //     ->with('info', trans('To complete your profile, we request you to upload a clear photo of yourself.'));
        }

        return $next($request);
    }
}


Solution

  • I have solved the problem as follows

    public function handle($request, Closure $next)
        {
            // Checking through relations if the User has already an image
            if (empty(auth()->user()->profile->image)) {
                if (!$request->routeIs('image.create')) {
                    if (!$request->isMethod('post')) {
                        return redirect()
                            ->route('image.create')
                            ->with('info', trans('To complete your profile, we request you to upload a clear photo of yourself.'));
                    }
                }
            }
            return $next($request);
        }