1. Go to this page and download the library: Download wstam/eloquentfilter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use WStam\EloquentFilter\EloquentFilterable;
class Product extends Model
{
use EloquentFilterable;
}
/**
* Show all the products
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::query()->orderBy('title', 'ASC');
// addFilter($column, $label = null, $type = 'text', $default = null, $comparison = '=')
$products->addFilter('title', 'Title', 'text');
$products->addFilter('external_supplier', 'External supplier', 'boolean');
// Use filterCollection instead of get() to implement the "renderFilter" method into the collection
$productCollection = $products->filterCollection();
return view('product.index', ['products' => $productCollection]);
}
<!-- This renders the filters. You can change the filter views at vendor/eloquentfilter -->
{!! $products->renderFilters() !!}
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Title</th>
<th>External supplier</th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>
<a href="{{ route('product_edit', $product->id) }}">{{$product->title}}</a>
</td>
<td>
@if($product->external_supplier == 1)
Yes
@else
No
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>