Laravel 4搜索功能验证

时间:2013-08-13 12:13:34

标签: laravel laravel-4

我遇到了问题。

我如何验证搜索功能?就像它找不到用户一样,它会显示错误信息。我尝试过几件事,但都没有。这是我的表格:

(搜索/ search.blade.php)

<form id="custom-search-form" class="form-search form-horizontal pull-right" action="{{ URL::action('CharactersController@search') }}" method="get">
<div class="input-append span9">
    <input type="text" class="search-query" name="character" placeholder="Character/guild name">
    <button type="submit" class="btn"><i class="icon-search"></i></button>
</div>

我尝试过这样的事情:@if ($searchResult->count()) - 它有效,但每次进入搜索网站时都会显示。

这是我的控制器的动作:

public function search()
{
    $name = Input::get('character');
    $searchResult = Player::where('name', '=', $name)->get();
    return View::make('search.search')
            ->with('name', $name)
            ->with('searchResult', $searchResult);
}

2 个答案:

答案 0 :(得分:1)

不应该是

@if(count($searchResult) > 0)
  //Show results?
@endif

答案 1 :(得分:0)

您可以将控制器方法拆分为getSearch()以显示搜索表单,然后使用postSearch()显示结果。

public function getSearch()
{
    return View::make('search.search');
}

public function postSearch()
{
    $name = Input::get('character');
    $searchResult = Player::where('name', '=', $name)->get();
       return View::make('search.search')
            ->with('name', $name)
            ->with('searchResult', $searchResult);
}

//search.blade example

@if(isset($searchResult))
    @if($searchResult->count() > 0)
        //display results
    @else
        //no results to display
    @endif
@endif