Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用AJAX在laravel中进行实时搜索_Jquery_Ajax_Laravel_Laravel 5_Laravel 5.4 - Fatal编程技术网

Jquery 使用AJAX在laravel中进行实时搜索

Jquery 使用AJAX在laravel中进行实时搜索,jquery,ajax,laravel,laravel-5,laravel-5.4,Jquery,Ajax,Laravel,Laravel 5,Laravel 5.4,我希望用户能够在任何页面上搜索和显示结果 导航栏中的搜索框如下所示: <form action="/search" method="get" autocomplete="off" class="navbar-form navbar-left"> <div class="form-group"> <input type="text" class="form-control" id="search_text" onkeyup="search_da

我希望用户能够在任何页面上搜索和显示结果

导航栏中的搜索框如下所示:

<form action="/search" method="get" autocomplete="off" class="navbar-form navbar-left">
    <div class="form-group">
        <input type="text" class="form-control" id="search_text" onkeyup="search_data(this.value, 'result'); placeholder="Search"></div>
        <div id="result">
            @include('results')
        </div>
    </div>
</form>
控制器:

public function search() {
    $search_text = $_GET['text'];
    if ($search_text==NULL) {
        $data= Business::all();
    } else {
        $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
    }
    return view('results')->with('results',$data);
}
路线:

Route::post('/searching', 'SearchController@search');
Results.blade.php

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
</table>
<table style="width:100%">
<tr>
    <th>Name</th>
    <th>Logo</th>
</tr>
@if (isset($results) && count($results) > 0)
@foreach( $results as $business )
<tr>
    <td>{{ $business->logo }}</td>
    <td>{{ $business->name}}</td>
</tr>
@endforeach
@endif

名称
标志
@foreach(结果为$business)
{{$business->logo}
{{$business->name}
@endforeach
这就是我到目前为止得到的结果,我得到了这个错误:

未定义变量:结果
(视图:C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(视图:C:\xampp\htdocs\laravel\resources\views\results.blade.php)
(视图:C:\xampp\htdocs\laravel\resources\views\results.blade.php)


任何人都可以帮助一个可怜的灵魂吗?

我不完全确定->with()的工作原理,但您可以通过以下两个选项之一直接向视图帮助器添加数据:

return view('results', ['results' => $data]);


请注意,最后一个选项将在您的视图中创建一个值为$data的变量$data。

您将在表单blade中包括结果视图,其中结果视图中的
结果变量最初不可用。您可以改为在“结果”视图中执行检查,以确保仅当数据可用时才在集合中循环

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @if (isset($results) && count($results) > 0)
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
    @endif
</table>
并以以下方式访问控制器中的搜索文本:

public function search($search = null) { $search_text = $search; if ($search_text==NULL) { $data= Business::all(); } else { $data=Business::where('name','LIKE', '%'.$search_text.'%')->get(); } return view('results')->with('results',$data); } 公共函数搜索($search=null){ $search\u text=$search; 如果($search\u text==NULL){ $data=Business::all(); }否则{ $data=Business::where('name','LIKE','%.$search_text.'%')->get(); } 返回视图('results')->带有('results',$data); } 控制器

public function search() {
$search_text = $_GET['search_text'];
if ($search_text==NULL) {
    $data= Business::all();
} else {
    $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
}
return view('results')->with('results',$data);}
Results.blade.php

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
</table>
<table style="width:100%">
<tr>
    <th>Name</th>
    <th>Logo</th>
</tr>
@if (isset($results) && count($results) > 0)
@foreach( $results as $business )
<tr>
    <td>{{ $business->logo }}</td>
    <td>{{ $business->name}}</td>
</tr>
@endforeach
@endif

名称
标志
@如果(设置($results)&&count($results)>0)
@foreach(结果为$business)
{{$business->logo}
{{$business->name}
@endforeach
@恩迪夫

它仍然说结果没有定义,问题是搜索框中有输入后应该返回结果,否则它当然是空的,因此这里没有明确定义:有:未捕获的语法错误:意外标记}它实际上指给我那个div结果,js似乎很好您的表单和结果。blade.php似乎很正常,没有任何bug,results.blade.phpmissing中还有其他代码吗“在onkeyup之后,这就是导致它的原因--我没有找到http,所以我的路由需要一个变量,它只是一个随机变量吗?你没有用ajax请求发送csrf令牌,你也可以使用像
url
这样的帮助器方法来生成url
 Route::get('/searching', 'SearchController@search');
public function search() {
$search_text = $_GET['search_text'];
if ($search_text==NULL) {
    $data= Business::all();
} else {
    $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
}
return view('results')->with('results',$data);}
<table style="width:100%">
<tr>
    <th>Name</th>
    <th>Logo</th>
</tr>
@if (isset($results) && count($results) > 0)
@foreach( $results as $business )
<tr>
    <td>{{ $business->logo }}</td>
    <td>{{ $business->name}}</td>
</tr>
@endforeach
@endif