Unifx Unifx - 2 years ago 108
SQL Question

Laravel 5.2 Checking user field on login

I have spent all day trying to find the best practice solution for this.
I want to check if the boolean field in the user table is true before letting the user see any other pages.

I am using Laravels built in auth and I have tried many solutions that are already on stack overflow and they dont work.

Answer Source

Create a middleware. Something like so:


namespace App\Http\Middleware;

use Closure;
use Auth;
use Redirect;

class UserHasField
     * Handle an incoming request.
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
    public function handle($request, Closure $next)
        if (Auth::user()->field === 0)
            return redirect()->route('named.route');

        return $next($request);

Now add your middleware class to kernel.php (in http) folder:

protected $routeMiddleware = [
    'user_has' => \App\Http\Middleware\UserHasField::class,

Now use it in your routes for example:

Route::group(['middleware' => ['user_has']], function () {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download