2016-10-25 5 views
0

Этот веб-сайт Book Hotel, после поиска пользователя, чтобы увидеть город и отель.Как создать Загрузка удаленного поиска данных в Laravel 5.2?

Я хочу использовать Загрузка удаленных данных select2 в laravel 5.2.

примеры

GitHub: https://select2.github.io/examples.html#data-ajax

Модель City:

protected $fillable = [ 
    'name', 'state', 'country', 'status' 
]; 

Модель Hotel:

protected $fillable =[ 
    'name', 'address', 'phone', 'status', ... 
]; 

HTML:

<select class="js-data-example-ajax"> 
    <option value="3620194" selected="selected">select2/select2</option> 
</select> 

Автор сценария:

$(".js-data-example-ajax").select2({ 
ajax: { 
url: "https://api.github.com/search/repositories", 
dataType: 'json', 
delay: 250, 
data: function (params) { 
    return { 
    q: params.term, // search term 
    page: params.page 
    }; 
}, 
processResults: function (data, params) { 
    // parse the results into the format expected by Select2 
    // since we are using custom formatting functions we do not need to 
    // alter the remote JSON data, except to indicate that infinite 
    // scrolling can be used 
    params.page = params.page || 1; 

    return { 
    results: data.items, 
    pagination: { 
     more: (params.page * 30) < data.total_count 
    } 
    }; 
}, 
cache: true 
}, 
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work 
minimumInputLength: 1, 
templateResult: formatRepo, // omitted for brevity, see the source of this page 
templateSelection: formatRepoSelection // omitted for brevity, see the source of this page 
}); 

Как написать в Laravel controller после отправки данных из ajax?

ответ

1

HTML:

<select id="hotel" name="hotel"> 
    <option value="hotel1">Hotel1</option> 
    <option value="hotel2">Hotel2</option> 
</select> 

Jquery и Ajax:

$(document).on('click','#hotel',function(e) 
{ 
    e.preventDefault(); 
    var name=$(this).val('hotel'); 

    $.ajax({ 
     type:"POST", 
     url: "{{url('/controller/search')}}", 
     data: { 
     "_token": "{{ csrf_token() }}" 
     }, 
     success: function (data) { 
      var res = $.parseJSON(data); 
      if(res == true) 
      { 
        alert('ok'); 
      } 
     } 
    }); 
}); 

Laravel Контроллер:

public function search(Request $request) 
{ 
    $hotel=Hotel::where(['name',$request->name])->first(); 
    return Response::json(array('status'=>TRUE,'hotel'=>$hotel)); 
} 
+0

Спасибо так много, но я хочу видеть после поиска города и отеля вместе – mySun

Смежные вопросы