I'm trying to filter date from ajax. But it seems I cannot pass parameter to model_function with this way.
In entity crud controller setup:
if(!empty($this->crud->request->from_to)) {
$dates = $this->crud->request->from_to; // getting filter value
$this->crud->addColumn([
'name' => 'stock_selled',
'label' => 'Bought',
'type' => 'model_function',
'function_name' => 'getBoughtStock("'.$dates.'")', // this parameter isn't working
]);
} else {
$this->crud->addColumn([
'name' => 'stock_selled',
'label' => 'Bought',
'type' => 'model_function',
'function_name' => 'getBoughtStock', // without parameter, it's working
]);
}
In model:
public function getBoughtStock($date = false) {
if($date == false) {
$boughtQty = StockDetail::
where('product_id', $this->id)
->where('status', 1)
->sum('received_qty');
}
else {
$dates = json_decode($date);
$dates->from = date('Y-m-d', strtotime($dates->from));
$dates->to = date('Y-m-d', strtotime($dates->to));
$boughtQty = StockDetail::
where('product_id', $this->id)
->where('status', 1)
->whereDate('created_at', '>=', $dates->from)
->whereDate('created_at', '<=', $dates->to)
->sum('received_qty');
}
return $boughtQty;
}
It should be total sum within the date filter.
In console, I got this response
"message": "Call to undefined method Illuminate\Database\Query\Builder::getBoughtStock(\"{\"from\":\"01-2-2018\",\"to\":\"16-2-2018\"}\")()
I tried another way to passing parameter with just number, simple string, array, etc, and it also doesn't work... I guess my way is wrong because I noticed there's another extra '()'
Backpack: 3.3.8
Laravel: 5.5
PHP: 7.1.7
Mysql: 5.7.19
IIRC you can't specify any dynamic values in the actual field definition.
You might be able to do something like:
public function getBoughtStock()
{
$date = request()->input('dates'); // OR whatever
// insert something more here
}
Please ask on stack overflow - feel free to add a link here as this is not a bug.
Most helpful comment
IIRC you can't specify any dynamic values in the actual field definition.
You might be able to do something like:
Please ask on stack overflow - feel free to add a link here as this is not a bug.