0

I'm using Laravel 5. How do I use the $_GET variables inside my controller? This is my HTML;

<form class="form-horizontal" role="form" method="GET" action="http://127.0.0.1:8080/contracts/refine">
    <div class="form-group">
        <label class="col-md-4 control-label">Postcodes:</label>
            <div class="col-md-6">
                <select name="postcode_dataset_id">
                  <option value="1">February 2015</option>
                  <option value="2">May 2015</option>
                  <option value="3">August 2015</option>
                </select>
            </div>
    </div>

    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary" style="margin-right: 15px;">Load</button>
        </div>
    </div>
</form>

When I submit the form it's going to my route.php;

Route::get('/contracts/refine', ['as' => 'postcodes-dataset-refine', 'uses' => 'ContractsController@refine']);

Now I want the result of the form in my Controller method so I can actually use it! So far I have this in my refine method in my controller;

public function refine()
{
    print $_GET['postcodes_dataset_id'];
}

But I keep getting this error and I don't know what it means

ErrorException in ContractsController.php line 44:
Undefined index: postcodes_dataset_id 

I tried to define it by going;

public function refine()
{
  $postcodes_dataset_id = "";
  print $_GET['postcodes_dataset_id'];
}

But that does nothing!

UPDATE - This is the top of my controller.. just to show you what namespaces I already have registered;

<?php namespace App\Http\Controllers;

use App\Http\Requests; 
use App\Http\Controllers\Controller;
use App\Models\Contract;
use App\Models\PostcodeLookup;
use App\Models\Location;
use App\Models\Lookup;
//use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Http\Request; 

class ContractsController extends Controller {
mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95
  • 1
    typo? `postcode_dataset_id` is you select name, you retrieve it with `$_GET['postcodes_dataset_id']` (see the extra 's' in 'postcodes') – giorgio Mar 30 '15 at 13:11

3 Answers3

3

Laravel map the request to an object to filter that input you shouldn't use $_GETdirectly in your controller.

$name = Request::input('name');

http://laravel.com/docs/5.0/requests

René Höhle
  • 26,716
  • 22
  • 73
  • 82
1

You can retrieve an input value for any HTTP verb by using Input::get().

for laravel 4 documentation

check the answer of bogartalamid

$name = Input::get('postcodes_dataset_id');

for laravel 5 document

$name = Request::input('postcodes_dataset_id');
Community
  • 1
  • 1
Ghostman
  • 6,042
  • 9
  • 34
  • 53
1

You should definitely retrieve input using the Request class.

Either with the facade: (make sure to add use Request; at the top of your class)

echo Request::input('postcode_dataset_id');

Or by injecting the request:

public function refine(\Illuminate\Http\Request $request)
{
    echo $request->input('postcode_dataset_id');
}

Also you seem to have a typo in there.

postcode_dataset_id !== postcodes_dataset_id
//                              ^
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • When I include `use Request` I get the error `Cannot use Illuminate\Http\Request as Request because the name is already in use` - which I don't understand – mikelovelyuk Mar 30 '15 at 13:31
  • 1
    If you want to use the facade, remove `use Illuminate\Http\Request;` if you have added that import statement because you need it give `Request` an alias name or use dependency injection right away – lukasgeiter Mar 30 '15 at 13:33
  • Ok.. It works :) What is the difference between `use Illuminate\Http\Request;` and `use Request;` though? I am new to namespaces and Laravel 5 is very confusing to me at the moment. – mikelovelyuk Mar 30 '15 at 13:41
  • There are two `Request` classes. One is `Illuminate\Http\Request` and the other `Illuminate\Support\Facades\Request`. The first one is the actual request class, the second is a [facade](http://laravel.com/docs/5.0/facades) that provides an easy access to the current request. Furthermore `Illuminate\Support\Facades\Request` has a global alias `Request`. This is what you're using when you write `use Request;`. – lukasgeiter Mar 30 '15 at 13:44