2017-02-19 62 views
2

我還在學習php和Laravel框架。我無法在我的控制器中使用這些代碼,而是將它們放在我的視圖中,但那不是我想要的。 我想要所有我的控制器的邏輯,但我不知道如何做到這一點。Laravel 5 scout

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

回答

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