I have a selectbox/dropdown in my sidebar.blade.php View:
{!! 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() !!}
The select box receives its values from model -> Blog.php :
public static function getCategories()
{
return self::groupBy('category')->lists('category', 'category');
}
On 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));
}
Routes:
Route::controller('/blog', '\Serverfireteam\blog\BlogController');
//Below line doesn't work (obviously)
Route::get('/blog/search',['as'=>'search','uses'=>'Serverfireteam\blog\BlogController@search']);
My main content (list of displayed posts) is iterated through the index.blade.php View as:
@foreach($last as $post)
This iterates appropriately for ALL blogs. I need to make this dynamic with the dropdown / select box. I am sure that I need AJAX (but this is my first project and I'm clueless)
Here's a small sample of what I have tried:
I know that I am close and it is my inexperience that's destroying me here.
My question: Please, where am I screwing up? I have spent days on this one issue.
** UPDATE Here, I updated my code to try something else; please take a look below
<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>
Here is a script that I currently have on my 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>
Here is an updated look at my 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));
}
Lastly, the routes.php
Route::get('/blog/search/{category}', <br>
['a s'=>'search','uses'=>'Serverfireteam\blog\BlogController@search']);
Here is the error I'm receiving
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: search] [URI: blog/search/{category}].
(View:web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)
(View: web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)
(View: web/hearten/vendor/serverfireteam/blog/src/views/sidebar.blade.php)