Laravel 雄辩的搜索表单查询(表单中的多个参数)


Laravel Eloquent Search Form Query (Multiple parameters in form)

我正在尝试使用Eloquent查询Laravel中的数据库。当我单击视图上的提交按钮时,我的页面没有进入结果页面,我相信我没有查询数据库。我认为我的控制器句柄索引或路由存在问题,因为我正在尝试将查询结果呈现到结果页面上。

有什么想法吗?

这是我的路线.php文件:

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

我的游戏控制器.php如下:

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');
        }
    }

最后,我的观点索引.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>

你的代码有什么问题

1) handleIndex()函数处理您的表单处理。但是,我在任何地方都看不到您的代码尝试使用该函数。

2) handleIndex()函数应该附加一条路由。

3)在您看来,您在没有提供URL的情况下{{ Form::open(array('method' =>'GET')) }}打开了表单,因此您的表单在同一页面上发送数据,即索引。

4) 视图表单的输入没有名称。给他们一个,否则他们的值不会被提交。

如何使它正确

路线.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');

查看索引.刀片.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> -->