路由和 window.location - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45859618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Routes and window.location - Laravel
提问by PioPie
I have a search field with the same name and id inside my categories page and inside my products page.The autocomplete suggestions seems to work fine , however once I click on search suggestion inside my search field it converts my search string to integer which is my products id .Afterwards its redirecting me to "mysite/products id" with "No content for this menu" . I guess the issue is with my routes and my window.location inside my JS , but I cant figure it out.
我的类别页面和产品页面中有一个具有相同名称和 ID 的搜索字段。自动完成建议似乎工作正常,但是一旦我单击搜索字段中的搜索建议,它会将我的搜索字符串转换为整数,这是我的产品 id 。之后它重定向我到“mysite/products id”与“此菜单没有内容”。我想问题出在我的路由和我的 JS 中的 window.location 上,但我无法弄清楚。
Here is my code
这是我的代码
My routes
我的路线
Route::get('products/{id}', 'AutoCompleteController@show');
Route::get('autocomplete',array('as'=>'autocomplete','uses'=>'AutoCompleteController@show'));
Route::get('searchajax',array('as'=>'searchajax','uses'=>'AutoCompleteController@autoComplete'));
Few more routes connected to Categories and Products :
连接到类别和产品的更多路线:
Route::get('shop', 'ShopController@categories');// my categories page
Route::get('shop/{category_url}', 'ShopController@products'); // my products page
My AutoCompleteController:
我的自动完成控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
class AutoCompleteController extends MainController {
public function show(Product $product) {
return view('content.products', ['product' => $product]);
}
public function autoComplete(Request $request) {
$query = $request->get('term','');
$products=Product::where('title','LIKE','%'.$query.'%')->get();
$data=array();
foreach ($products as $product) {
$data[]=array('label'=>$product->title,'value'=>$product->id);
}
if(count($data)){
return $data;}
else{
return ['value'=>'No Result Found','id'=>''];
}}
}
My view in products.blade and categories.blade for my autocomplete search is the same :
我在 products.blade 和 Categories.blade 中对自动完成搜索的看法是一样的:
@extends ('master')
@section('content')
<link href="http://demo.expertphp.in/css/jquery.ui.autocomplete.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="row">
<form class="navbar-form text-center " form method="GET" action=" ">
<input id="search_text" placeholder=" Search products" name="search_text" type="text" value="" style="width: 400px; height: 35px; border-radius: 5px ; padding-left: 12px;"><br><br>
<input class="btn btn-default " type="submit" value=" Search" >
</form>
</div>
<script>
$(document).ready(function () {
src = "{{ route('searchajax') }}";
$("#search_text").autocomplete({
source: function (request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
term: request.term
},
success: function (data) {
response(data);
}
});
},
minLength: 3,
select: function( event, ui ) {
window.location = '{{ url('shop/{category_url}')}}' + ui.item.value
} // not sure if this is the correct way , please advise
});
});
</script>
回答by Don't Panic
In your autoComplete
method:
在你的autoComplete
方法中:
foreach ($products as $product) {
// I guess you have product->cagegory relationship, and category model
// has a URL?
$category_url = $product->category->url;
$data[]=array('label'=>$product->title,'value'=>$category_url);
}
Now your autocomplete results include the category_url, so you just update your JS:
现在您的自动完成结果包括 category_url,所以您只需更新您的 JS:
window.location = '{{ url("shop/" + ui.item.value) }}';