How To Pass GET Parameters To Laravel From With GET Method ?

后端 未结 7 899
余生分开走
余生分开走 2020-11-30 02:05

i\'m stuck at this very basic form, that i could not accomplish, which i want to build a search form with an text input, and two select controls, with a route that accept 3

7条回答
  •  抹茶落季
    2020-11-30 02:38

    I was struggling with this too and finally got it to work.

    routes.php

    Route::get('people', 'PeopleController@index');
    Route::get('people/{lastName}', 'PeopleController@show');
    Route::get('people/{lastName}/{firstName}', 'PeopleController@show');
    Route::post('people', 'PeopleController@processForm');
    

    PeopleController.php

    namespace App\Http\Controllers ;
    use DB ;
    use Illuminate\Http\Request ;
    use App\Http\Requests ;
    use Illuminate\Support\Facades\Input;
    use Illuminate\Support\Facades\Redirect;
    
        public function processForm() {
            $lastName  = Input::get('lastName') ;
            $firstName = Input::get('firstName') ;
            return Redirect::to('people/'.$lastName.'/'.$firstName) ;
        }
        public function show($lastName,$firstName) {
            $qry = 'SELECT * FROM tableFoo WHERE LastName LIKE "'.$lastName.'" AND GivenNames LIKE "'.$firstName.'%" ' ;
            $ppl = DB::select($qry);
            return view('people.show', ['ppl' => $ppl] ) ;
        }
    

    people/show.blade.php

    Notes:
    I needed to pass two input fields into the URI.
    I'm not using Eloquent yet, if you are, adjust the database logic accordingly.
    And I'm not done securing the user entered data, so chill.
    Pay attention to the "_token" hidden form field and all the "use" includes, they are needed.

    PS: Here's another syntax that seems to work, and does not need the

    use Illuminate\Support\Facades\Input;
    

    .

    public function processForm(Request $request) {
        $lastName  = addslashes($request->lastName) ;
        $firstName = addslashes($request->firstName) ;
        //add more logic to validate and secure user entered data before turning it loose in a query
        return Redirect::to('people/'.$lastName.'/'.$firstName) ;
    }
    

提交回复
热议问题