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 parameters, the problem that when the i submit the form, it map the parameters with the question mark, not the Laravel way,
{{ Form::open(['route' => 'search', 'method' => 'GET'])}} <input type="text" name="term"/> <select name="category" id=""> <option value="auto">Auto</option> <option value="moto">Moto</option> </select> {{ Form::submit('Send') }} {{ Form::close() }}
Route::get('/search/{category}/{term}', ['as' => 'search', 'uses' => 'SearchController@search']);
When i submit the form it redirect me to
search/%7Bcategory%7D/%7Bterm%7D?term=asdasd&category=auto
How can i pass these paramters to my route with the Laravel way, and without Javascript ! :D
To retrieve the query parameters on your Laravel backend, you can make use of either the "Request" class or the "request()" helper method. Imagine you want to get the "search" query from the URL, you can do as follows. $searchQuery = $request->query('search');
Basically what you need to understand is that get() return a collection(note that one object can be in the collection but it still a collection) why first() returns the first object from the result of the query(that is it returns an object) #Take_away Get() return a collection first() return an object.
input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).
You can add this package by adding it to the require section in your composer. json file. Then run composer update to get it installed. Then in Laravel you can wrap it in a class (perhaps a repository-like class) that handles making API request and returning data for your app to use.
The simplest way is just to accept the incoming request, and pull out the variables you want in the Controller:
Route::get('search', ['as' => 'search', 'uses' => 'SearchController@search']);
and then in SearchController@search
:
class SearchController extends BaseController { public function search() { $category = Input::get('category', 'default category'); $term = Input::get('term', false); // do things with them... } }
Usefully, you can set defaults in Input::get()
in case nothing is passed to your Controller's action.
As joe_archer says, it's not necessary to put these terms into the URL, and it might be better as a POST (in which case you should update your call to Form::open()
and also your search route in routes.php - Input::get()
remains the same)
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
<form method="post" action="/people"> <input type="text" name="firstName" placeholder="First name"> <input type="text" name="lastName" placeholder="Last name"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <input type="submit" value="Search"> </form>
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) ; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With