0

I'm trying to find the correct way to lay this out in laravel so that I can hit one GET route which calls a single function, but within that function I want to use data from a mysql table to determine which blade to show.

Say this URL is visited with a query string parameter:

www.testsite.com?email=testEmail.com

I hit this route (But not sure how to accept the parameter)

Route::get('register', 'Data\DataController@DataForm')
    ->name('Data.register');

I have a mysql table called dataTable set up like so

email            |  type
-------------------------
test1@mail.com      A
test2@mail.com      B
test3@mail.com      C

What's the best way to incorporate the email parameter so that I can hit the single route and single function, then use the email/type columns from mysql to determine the appropriate blade to show?

public function DataForm(Request $request)
{

    //query table based on query string parameter 'email'

    $email = dataTable::where('email', /*email parameter?*/)->first();

    if($email['type']== A){

        return view('data.typeA');

    }elseif($email['type']== B){

        return view('data.typeB');

    }elseif($email['type']== C){

        return view('data.typeC');

    }
}
bassxzero
  • 4,838
  • 22
  • 34
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • This might be a dup. Maybe this will be useful to you. https://stackoverflow.com/questions/27298426/how-to-pass-get-parameters-to-laravel-from-with-get-method – bassxzero Feb 18 '19 at 19:46

4 Answers4

2

You can add it as a route parameter :

Route::get('register/{email}', 'Data\DataController@DataForm')->name('Data.register');

And then inside controller :

public function DataForm($email)
{
    // Abort to 404 if $email is not a valid email address
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        abort(404);
    }

    $typeData = Model::where('email', $email)->first();

    // Email not found in the database
    if(!$typeData){
        abort(404);
    }

    switch ($typeData->type) {
        case 'A':
            return view('data.typeA');
        case 'B':
            return view('data.typeB');
        case 'C':
            return view('data.typeAC');
        default:
            abort(404);
    }
}
Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
1

Using a slash in the route you should be able to pass it like this:

Route::get('register/{email}', 'Data\DataController@DataForm')
    ->name('Data.register');

Then in your function you can just use the variable $email and it contains the email in the URL. But not sure if this URL is OK, using a slash instead of '?email=' for the passing of the parameter.

More on there here:

https://laravel.com/docs/5.7/routing#required-parameters

techcyclist
  • 376
  • 1
  • 11
1

You'd need more logic than this, obviously:

public function DataForm(Request $request)
{
    $type = (dataTable::where('email', $request->query->get('email'))->first())['type'];

    return view('data.type'.$type);

}
keyboardSmasher
  • 2,661
  • 18
  • 20
  • I think this looks about right, however, say the blade doesn't actually end up with they type in the name, I could just use the if logic with $type['type'] right? – Geoff_S Feb 18 '19 at 20:14
  • You need to figure out what to do if the email isn't in the database or the blade template doesn't exist, etc. Just standard stuff. :) – keyboardSmasher Feb 18 '19 at 20:41
0

As others said you could auto type in Request $request; however you don't have to. You can keep the function definition without the passed parameter, and use the helper functions instead:

if(request()->has('email')) {
    $email = request()->input('email');
    // Do stuff
} else {
    // No email supplied
}
J. A. Streich
  • 1,683
  • 10
  • 13