Laravel Eloquent 搜索表单查询(表单中有多个参数)
Laravel Eloquent Search Form Query (Multiple parameters in form)
我正在尝试使用 Eloquent 查询 Laravel 中的数据库。当我单击视图上的提交按钮时,我的页面不会转到结果页面,而且我相信我不会查询数据库。我认为我的控制器 handleIndex 或我的路由有问题,因为我正在尝试将查询结果呈现到结果页面上。
有什么想法吗?
这是我的 Routes.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');
我的GameController.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');
}
}
最后,我的看法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>
你的代码有什么问题
1) handleIndex()
函数处理您的表单处理。但是,我在任何地方都没有看到您的代码试图使用该功能。
2) handleIndex()
函数应该附加一个路由。
3) 在您看来,您打开表单 {{ Form::open(array('method' =>'GET')) }}
时未提供 URL,因此您的表单在同一页面上发送数据,即索引。
4) 您的视图表单输入没有名称。给他们一个,否则他们的值不会被提交。
如何正确设置
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');
查看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> -->
我正在尝试使用 Eloquent 查询 Laravel 中的数据库。当我单击视图上的提交按钮时,我的页面不会转到结果页面,而且我相信我不会查询数据库。我认为我的控制器 handleIndex 或我的路由有问题,因为我正在尝试将查询结果呈现到结果页面上。
有什么想法吗?
这是我的 Routes.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');
我的GameController.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');
}
}
最后,我的看法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>
你的代码有什么问题
1) handleIndex()
函数处理您的表单处理。但是,我在任何地方都没有看到您的代码试图使用该功能。
2) handleIndex()
函数应该附加一个路由。
3) 在您看来,您打开表单 {{ Form::open(array('method' =>'GET')) }}
时未提供 URL,因此您的表单在同一页面上发送数据,即索引。
4) 您的视图表单输入没有名称。给他们一个,否则他们的值不会被提交。
如何正确设置
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');
查看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> -->