我试图使用Eloquent在Laravel中查询数据库。当我单击我的视图上的提交按钮时,我的页面不会进入结果页面,我相信我不会查询数据库。我认为我的Controller 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>
答案 0 :(得分:2)
您的代码有什么问题
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> -->