Laravel 5侦察兵

时间:2017-02-19 14:26:07

标签: php laravel-5 algolia laravel-scout

我还在学习php和Laravel框架。我不能让这个代码在我的控制器中工作而是将它们放在我的视图中,但这不是我想要的。 我想在控制器中使用我的所有逻辑,但我无法弄清楚如何做到这一点。

// in my controller
public function index(Request $request) {
    $error = ['error' => 'No results found, please try with different keywords.'];
    $results = Actor::search($request->get('query'))->get();
    $results->count() ? $results : $error;
    return view('welcome', compact('results', 'request', 'error'));
}

// in my view
@if($request->has('query'))

@foreach($results as $result)
    {{$result->first_name}}  
    {{$result->last_name}}
@endforeach
@else
    <p> Please Enter a Keyword</p> 

1 个答案:

答案 0 :(得分:0)

您可以尝试:

public function help(Request $request){

    $results = Actor::search($request->get('query'))->get();

    return view('welcome', compact('results', 'request'));
}

在你看来:

@if($request->has('query'))

    @if($results->count() > 1)

        @foreach($results as $result)
            {{$result->first_name}} &nbsp;
            {{$result->last_name}}
        @endforeach

    @else
        'No results found, please try with different keywords.'
    @endif

@else
    <p> Please Enter a Keyword</p> 
@endif