2

我还在学习 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> 
4

1 回答 1

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
于 2017-02-24T18:44:15.503 回答