//the html
<label>Show</label>
<select id="selectAmount">
<option id="tweleve" value="12" selected="selected">12</option>
<option id="fifteen" value="15">15</option>
<option id="thirty" value="30">30</option>
</select>
//the default number of products is 12
<div class="products">
<ul>
@foreach($products as $product)
<li></li>
<li></li>
<li></li>
@endforeach
</ul>
</div>
// the route
Route::get('products', 'ProductController@index');
//the controller
class ProductController extends Controller
{
public function index(Request $request)
{
$perpage = $request->input('amount');
$products = Product::paginate($perpage);
return view('pages.products', compact('products'));
}
}
//the ajax request
$(document).ready(function(){
$('#selectAmount').change( function() {
var selected = $('#selectAmount option:selected');
var myurl = '/products';
if(selected.val() <= 30) {
var amount = selected.val();
$.ajax({
type: 'GET',
url: myurl,
data: {amount:amount}
});
});
from Latest question asked on Laravel tag.
via Mostafa Esmat