Php Laravel Ajax搜索查询参数问题
尝试使用Laravel创建/学习ajax搜索结果。Im处于下一阶段,我可以从DB获取数据,但查询字符串似乎没有被捕获到控制器中并查询数据。我认为这是我的Ajax的问题,或者我将数据从视图发布到URL的位置。我是拉雷维尔的新手,所以欢迎任何建议。谢谢 search.blade.phpPhp Laravel Ajax搜索查询参数问题,php,laravel,Php,Laravel,尝试使用Laravel创建/学习ajax搜索结果。Im处于下一阶段,我可以从DB获取数据,但查询字符串似乎没有被捕获到控制器中并查询数据。我认为这是我的Ajax的问题,或者我将数据从视图发布到URL的位置。我是拉雷维尔的新手,所以欢迎任何建议。谢谢 search.blade.php <form class="navbar-search"> <div class="input-group&quo
<form class="navbar-search">
<div class="input-group">
<input type="text" class="form-control bg-lightblue border-0 small text-white border-dark" name="search" id="search" placeholder="Search for..." aria-label="Search" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-success" type="button"></button>
</form>
<div class="col-md-12">
<table class="table table-hover table-responsive-sm">
<thead class="thead-dark">
<tr>
<th scope="col">Total Data : <span id="total_records"></span></th>
<th scope="col">Company Name</th>
<th scope="col">Immediate Contact</th>
<th scope="col">Address</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
fetch_customer_data();
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('search.action') }}",
//url: 'user-figures/action',
method: 'GET',
data: {"query":query},
dataType:'json',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
});
</script>
我使用了您的代码并遵循了本教程,并且已经实现了这一点。这是我的全部代码,希望你能想出如何让你的工作 搜索控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Product;
class SearchController extends Controller
{
public function index()
{
return view('search.index');
}
public function search(Request $request)
{
if($request->ajax())
{
$output="";
$products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get();
if($products)
{
foreach ($products as $key => $product) {
$output.='<tr>'.
'<td>'.$product->id.'</td>'.
'<td>'.$product->title.'</td>'.
'<td>'.$product->description.'</td>'.
'<td>'.$product->price.'</td>'.
'</tr>';
}
return Response($output);
}
}
}
}
搜索操作应该是post路径而不是get路径吗?在ajax操作方法中,您是否应该将结果返回到视图?(我也是拉威尔的新手)路线应该是正确的,因为我是从一个教程和这个网站上的一些信息中得到的。结果应该返回到视图,如果没有查询,它将只返回表中的所有数据。数据正在从数据库返回,但查询字符串在某处丢失。感谢响应,我可以让它工作,但它使用了不同的函数,我认为现代Laravel应该通过返回json响应来完成。我还试图在搜索尚未启动时加载所有数据。我对此非常陌生,因此尝试从本基础教程中拼凑一些函数/结果。
Route::get('/search', 'figuresController@ajaxindex')->name('search');
Route::get('/search/action', 'figuresController@ajaxaction')->name('search.action');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Product;
class SearchController extends Controller
{
public function index()
{
return view('search.index');
}
public function search(Request $request)
{
if($request->ajax())
{
$output="";
$products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get();
if($products)
{
foreach ($products as $key => $product) {
$output.='<tr>'.
'<td>'.$product->id.'</td>'.
'<td>'.$product->title.'</td>'.
'<td>'.$product->description.'</td>'.
'<td>'.$product->price.'</td>'.
'</tr>';
}
return Response($output);
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="_token" content="{{ csrf_token() }}">
<title>Live Search</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Products info </h3>
</div>
<div class="panel-body">
<div class="form-group">
<input type="text" class="form-controller" id="search" name="search"></input>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('search/action')}}',
data:{'search':$value},
success:function(data){
$('tbody').html(data);
}
});
})
</script>
<script type="text/javascript">
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
</body>
</html>
Route::get('/search','SearchController@index');
Route::get('/search/action','SearchController@search')->name('search.action');