dongqianzong4275
2015-03-23 18:58
浏览 81
已采纳

使用Laravel手动自动完成搜索,如何将信息传递给视图中的div

Im trying to make a autocomplete search, that searches from a string in a input, in my database from several tables, like Persons, Users, and Bands. Using Laravel 4.2.

I then want three objects to be accessed, and updated, in a #div on the view im already at.

I have tried alot, but this one was hard. My solution worked on localhost but not online, but it was a bad solution anyway, by passing a view with->(the objects). Failed online since headers already are sent.

I think my solution should be something like this:

View:

{{ Form::open(['action' => ['SearchController@search'], 'method' => 'post']) }}
{{ Form::text('term', '', ['class' => 'uk-width-1-1', 'id' =>  'term', 'placeholder' =>  'Søk etter person'])}}
{{ Form::close() }}

Controller:

   public function search(){
    $term = Input::get('term');

    $fest = Session::get('festival');
    $festival = Festival::find($fest);

        $persons = DB::table('fs_persons')
            ->join('fs_festival_persons', 'person_id','=','fs_persons.id')
            ->where('festival_id', '=', $fest)
            ->get();  

    foreach ($persons as $query)
    {
        if ((strpos(strtolower($query->firstname), strtolower($term)) !== false) OR ((strpos(strtolower($query->lastname), strtolower($term)) !== false))) {
           $results[] = [ 'id' => $query->id, 'value' => $query->firstname.' '.$query->lastname, 'tlf' => $query->tlf ];
        }

}
return Response::json($results);
}

But im unsure how to use it properly, here is just one object, tried making a multilevel array with objects, but it is all very confusing for me. This is alittle bit code from the jquery autocomplete, i get that working but I want my own presentation of the search results.

I would love to be able to do, in a view, that gets updated on keyup on the search input, and where i can use blade templating:

@if($persons)
 @if(sizeof($persons)>0)
    <div class="uk-width-small-4-4">
    Personar:
    </div>
    <div class="uk-width-small-4-4">
    @foreach($persons as $person)
    <?php
                $tmp_pers = Person::find($person->id);
?>

    <span class="search_name">
    <a href='{{ URL::to("festival/$festivalen->slug/person/$person->id") }}'>
    {{ $person->firstname }} {{ $person->lastname }}
    </a>
    </span>
</div>
@endforeach
@endif
@endif

But im unsure on how to makeetrieve variables out of json, all this was much harder than I expected, so hoping anyone could guide me, thanks!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题