Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jacob Herper
Jacob Herper
91,103 Points

Laravel 5.2: route depending on form input

I have one email input field. After submitting the form, I want to check if the entered email address is already in at least one of three tables in the database (users, temp_users or customers). Finally there are 5 different possibilities: 1.) The email is in none of the tables, then it will include it in table A. 2.) The email is already in table A, -> include it in table B. 3.) The email is already in table A but value confirmed is false -> error page 4.) The email is already in table B, -> include it in table C. 4.) The email is already in table C -> show a login form!

I tried to accomplish that using Middleware but it didn't work. Here is my code:

public function handle($request, Closure $next)
    {
        $email = $request->email;
        $customer = Customer::where('billingEmail', '=', $email)->first();
        if($customer != null){
            return redirect('login/customer')->with('customer', $customer);
        }
        else{
            $user = User::where('email', '=', $email)->first();
            if($user != null){
                return redirect('register/customer')->with('user', $user); 
            }
            else{
                $tempUser = TempUser::where('email', '=', $email)->first();
                if($tempUser != null){
                    if($tempUser->confirmed){
                        // VERIFIED -> MAKE USER
                        return redirect('register/user')->with('tempUser', $tempUser); 
                    }
                    else{
                        // TEMPUSER -> NOT VERIFIED
                        return redirect('email/verify')->with('tempUser', $tempUser);
                    }
                }
                else{
                    // MAKE TEMPUSER
                    return redirect('email')->with('email', $email); 
                }
            }
        }
        return $next($request);
    }

Routing:

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'Auth\TempUserController@email');

 // Authentication Routes
    Route::get('login', 'Auth\TempUserController@email');
    Route::post('login', ['middleware' => 'login', function(){
     return 1;
    }]);

    Route::post('email', 'Auth\TempUserController@create');
    Route::post('email/verify', 'Auth\TempUserController@verifymsg');
    Route::post('register/user', 'Auth\TempUserController@user');
    Route::post('register/customer', 'Auth\AuthController@customer');
    Route::post('login/customer', 'Auth\AuthController@login');

 Route::get('verify', function(){ return view('errors.404'); });
 Route::get('verify/{token}', 'Auth\TempUserController@verify');

});