使用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!

There is a example project you can follow. laravel-smart-search

also should exist bellow code set in your view and you can define values in "select:" which you want. when user select the any suggestion other element will change.

View

$(function() {
      $("#search_text").autocomplete({
          source: "{{URL::route('searchajax')}}",
          minLength: 2,
          select: function( event, ui ) {
              $('#data').val(ui.item.id);
              $('#search_text').val(ui.item.value);
              $('#search').hide();
          }
      });
  });

Controller

foreach ( $persons as $query ){
        $data[] = array('value' => $row->$column, 'id' =>$row->id);
}
return Response::json($data);

your view 

<script>
  <?php foreach($Prods as $Pr)
  {
      $arr[]=$Pr->Pname;
  }
  ?>
  $( function() {
  var availableTags = <?php echo json_encode($arr); ?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );
  </script>

and your conrtoller

public function create()
    {   
        $Products = DB::table('Products')->get();
        $Prods=DB::table('Products')->select('Pname')->get();
        return view('test', ['Products'=>$Products],['Prods'=>$Prods]);
       }