Stackwhoyouknow
Stackwhoyouknow

Reputation: 61

Laravel Eloquent Search Form Query (Multiple parameters in form)

I'm trying to query a DB in Laravel using Eloquent. When I click the submit button on my view my page is not going to the results page and I believe i'm not querying the database. I think there's an issue with my Controller handleIndex or my routes because i'm trying to render the query results onto the results page.

Any ideas?

Here's my Routes.php file:

//Bind route parameters.
Route::model('game', 'Game');

// Show pages.
 Route::get('/', 'GameController@index');
 Route::get('/results', 'GameController@results');
 Route::get('/profile/{$Game}', 'GameController@profile');

My GameController.php is as follows:

public function index()
    {
        //show view of homepage
        return View::make('index');
        }

    public function handleIndex()
    {   
        //Handle each query from index form submissions
        $Game = Game::query();

        if(Input::has('Industry_Type'))
        {
            $Game->where('Industry_Type', Input::get('Industry_Type'));
        }
        if(Input::has('Office_State'))
        {
            $Game->where('Office_State', Input::get('Office_State'));
        }
        if(Input::has('Local_Name'))
        {
            $Game->where('Local_Name', Input::get('Local_Name'));
        }

        return View::make('results');
        }

    public function results(Games $game)
    {
        //show search results from index form

        return View::make('results', compact('game'));
        }

    public function profile()
    {
        //show individual game proifile
        return View::make('profile');
        }

    }

Lastly, my views index.blade.php:

<div class='main_search_form'> 


    <!--<form action="login" method="get"> -->

        {{ Form::open(array('method' =>'GET')) }}
        <div>
            <div>Find a Game to Join. Love the Work you do.</div>
                <div>Get Started</div>
                <select id="jobclass">
                    <option id="jobclass" name="jobclass" value="" disabled selected>What Type of Work Do You Do?</option>
                    <option>Truck Driver</option>
                    <option>Web Developer</option>
                </select>
            </div>
            <div>
                <div>In</div>
                    <input type="text" placeholder="Enter State" name="">
            <div>
                <div>Search by Game</div>   
                    <input type="text" placeholder="Name A Game" name="">
            </div>
        <div>   
            {{Form::submit('Find A Game', array('class' => 'btn')) }}
        </div>
        {{ Form::close() }}
      <!--  </form> -->
   </div>

Upvotes: 0

Views: 10530

Answers (1)

Mysteryos
Mysteryos

Reputation: 5791

What's wrong with your code

1) handleIndex() function handles your form processing. However, nowhere do i see your code attempting to use that function.

2) handleIndex() function should have a route attached to it.

3) In your view, you opened your form {{ Form::open(array('method' =>'GET')) }} without providing a URL, so your form was sending data on the same page, i.e index.

4) Your view form's inputs doesn't have a name. Give them one, else their values won't be submitted.

How to make it right

Routes.php

// Show pages.
Route::get('/', 'GameController@index');
Route::get('/results', 'GameController@results');
Route::get('/profile/{$Game}', 'GameController@profile');
//Form route handler
Route::get('/handle-index','GameController@handleIndex');

View index.blade.php

<div class='main_search_form'> 
    <!--<form action="login" method="get"> -->
    <!-- Added url parameter to form open -->
    {{ Form::open(array('method' =>'GET','url'=>'/handle-index')) }}
    <div>
        <div>Find a Game to Join. Love the Work you do.</div>
            <div>Get Started</div>
            <select id="jobclass">
                <option id="jobclass" name="jobclass" value="" disabled selected>What Type of Work Do You Do?</option>
                <option>Truck Driver</option>
                <option>Web Developer</option>
            </select>
        </div>
        <div>
            <div>In</div>
                <input type="text" placeholder="Enter State" name="">
        <div>
            <div>Search by Game</div>   
                <input type="text" placeholder="Name A Game" name="">
        </div>
    <div>   
        {{Form::submit('Find A Game', array('class' => 'btn')) }}
    </div>
    {{ Form::close() }}
  <!--  </form> -->

Upvotes: 2

Related Questions