0

I've got an problem.

How could I validate an search function? Like if it fails to find the user, it'll display an error message. I've tried several things, but none of them works. Here's my form:

(search/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>

I've tried to do something like: @if ($searchResult->count()) - it works, but it displays every time I enter the search site.

Here's my controller's acction:

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

2 回答 2

1

难道不应该

@if(count($searchResult) > 0)
  //Show results?
@endif
于 2013-08-13T12:20:00.967 回答
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  
于 2013-08-13T12:36:21.370 回答