我的sidebar.blade.php中有一个选择框/下拉列表查看:
{!! Form::open(array('method'=>'patch','route'=>'search')) !!}
{!! Form::select('search',$categories,null,array_merge(['class'=>'postform ts-select'],['placeholder'=>'Select Category'],['id'=>'search'],['name'=>'search'])) !!}
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3 col-centered" style="margin-top: 15px">
{!! Form::submit('Search', array('class'=>'blog-read ts-button')) !!}
</div>
</div>
{!! Form::close() !!}
选择框从模型中接收其值 - &gt; Blog.php:
public static function getCategories()
{
return self::groupBy('category')->lists('category', 'category');
}
在BlogController.php上:
public function getIndex()
{
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
//echo'<pre>';
//dd($mostRecommended);
return View('blog::index', array('title' => "Welcome ", 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
public function search($category)
{
$query = Request::get('search');
$articles = DB::table('blog')->where('category', '=', $query);
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
return View('blog::index', array('title' => $query, 'articles' => $articles, 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
路线:
Route::controller('/blog', '\Serverfireteam\blog\BlogController');
//下线不起作用(很明显)
Route::get('/blog/search',['as'=>'search','uses'=>'Serverfireteam\blog\BlogController@search']);
我的主要内容(显示的帖子列表)通过index.blade.php迭代查看:
@foreach($last as $post)
这适用于所有博客。我需要使用下拉/选择框使其动态化。我确信我需要AJAX(但这是我的第一个项目,我很无能)
这是我尝试过的一小部分样本:
** 更新 在这里,我更新了我的代码以尝试其他内容;请看下面的
<div id="categories-6" class="widget widget_categories">
<div class="title-widget"><h3>Categories</h3></div>
<label class="screen-reader-text" for="cat">Categories</label>
{!! Form::open(array('method'=>'patch','route'=>'search')) !!}
{!! Form::select('search',$categories,null,array_merge(['class'=>'postform ts-select'],['placeholder'=>'Select Category'],['id'=>'search'],['name'=>'search'])) !!}
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3 col-centered" style="margin-top: 15px">
{!! Form::submit('Search', array('class'=>'blog-read ts-button')) !!}
</div>
</div>
{!! Form::close() !!}
</div>
这是我目前在index.blade.php上的一个脚本
<script>
$('#search').on('change', function () {
var category = $(this).val();
var base_url = $({!! json_encode(url('/')) !!}).val;
$.ajax({
url: base_url + "/blog/search/" + category,
dataType : "json",
success: function (data) {
$('#inner-container').html(data.html); //here container is the wrapper of index view
}
});
});
</script>
以下是我的BlogController.php的更新资料
public function getIndex()
{
$mostRecommended = \App\Blog::mostRecommended();
$last = \App\Blog::lastPosts();
$categories = \App\Blog::getCategories();
//echo'<pre>';
//dd($mostRecommended);
return View('blog::index', array('title' => "Welcome ", 'last' => $last, 'mostRecommended' => $mostRecommended, 'categories' => $categories));
}
最后,routes.php
Route::get('/blog/search/{category}', <br>
['a s'=>'search','uses'=>'Serverfireteam\blog\BlogController@search']);
以下是我收到的错误
ErrorException in UrlGenerationException.php line 17:
缺少[路线:搜索] [URI:博客/搜索/ {类别}]所需的参数。
(查看:网络/振奋/供应商/ serverfireteam /博客/ src目录/视图/ sidebar.blade.php)
(查看:web / hearten / vendor / serverfireteam / blog / src / views / sidebar.blade.php)
(查看:web / hearten / vendor / serverfireteam / blog / src / views / sidebar.blade.php)