PHP code example of asemalalami / laravel-advanced-filter
1. Go to this page and download the library: Download asemalalami/laravel-advanced-filter 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/ */
asemalalami / laravel-advanced-filter example snippets
class Order extends Model
{
use HasFilter;
protected $casts = [
'void' => 'boolean',
];
public function channel()
{
return $this->belongsTo(Channel::class);
}
public function orderLines()
{
return $this->hasMany(OrderLine::class);
}
public function setupFilter()
{
$this->addField('void'); // will cast to 'boolean' from the model casts
$this->addField('total')->setDatatype('numeric');
$this->addFields(['source', 'subsource', 'order_date']);
// field from relation
$this->addFields(['channel.created_at' => 'channel_create'])->setDatatype('date');
$this->addField('orderLines.product.sku', 'product_sku');
// field from relation count
$this->addCountField('orderLines');
// custom field (raw sql)
$this->addCustomField('my_total', '(shipping_cost + subtotal)');
// enable general search
$this->addGeneralSearch(['source', 'orderLines.product.sku'], 'startsWith');
}
// customize field filter by custom scope
public function scopeWhereSource(Builder $builder, Field $field, string $operator, $value, $conjunction = 'and')
{
if ($operator == 'Equal') {
return $builder->where(function (Builder $builder) use ($value) {
$builder->where('source', $value)
->orWhere('subsource', $value);
});
}
// default behavior
return $builder->applyOperator($operator, $field, $value, $conjunction);
}
}
...
class OrderController extends Controller
{
public function index()
{
return Order::filter()->paginate(); // you can pass your custom request
}
}
public function scopeWhereEmail(Builder $builder, Field $field, string $operator, $value, $conjunction = 'and')
OrderLine.php
public function scopeWherePrice(Builder $builder, Field $field, string $operator, $value, $conjunction = 'and')
OR
Order.php
public function scopeWhereOrderLines(Builder $builder, Field $field, string $operator, $value, $conjunction = 'and')