Reputation: 63
I wrote my custom middleware, but when it is executed, the error appears. Middleware:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Employee;
class CheckConfirm
{
public function handle($request, Closure $next)
{
if(Auth::check())
{
$id = Auth::id();
$empl = Employee::where('user_id','=', $id)->get();
if($empl->isEmpty())
{
return route('confirm');
}
else
{
dump($empl);
return $next($request);
}
}
else
{
return route('login');
}
}
}
When I try something like this:
if($empl===null)
{
return route('confirm');
}
сondition just doesn't work. In this case, database queries are executed successfully. Here is the error page with dump
Upvotes: 4
Views: 13311
Reputation: 21
It is very hard to fix, I tried to do it with App\Http\Middleware\VerifyCsrfToken::except, but not work.
My solution to this problem was creating a redirect to another route using App\Exception\Handler::render method.
if ($exception->getMessage() == "Trying to get property 'headers' of non-object") {
return redirect()->route('my.default.page');
}
Upvotes: 1
Reputation: 62248
Your middleware must return a Response
object, or $next($request)
. As written, when not logged in or when $empl
is empty, your middleware is just returning a string, not a redirect.
Update your returns to:
return redirect()->route('confirm');
and
return redirect()->route('login');
respectively.
Upvotes: 8
Reputation: 125
Replace this line:
$empl = Employee::where('user_id','=', $id)->get();
if($empl->isEmpty()){ ... }
With this:
$empl = Employee::where('user_id', $id)->first();
if($empl){ ... }
Then dd() for each line, see where it fails. There may be an missing csrf token or the user is not logged..
Upvotes: 0