Crud: Passing parameter from model_function

Created on 15 Feb 2018  路  1Comment  路  Source: Laravel-Backpack/CRUD

Bug report

I'm trying to filter date from ajax. But it seems I cannot pass parameter to model_function with this way.

What I did:

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;
}

What I expected to happen:

It should be total sum within the date filter.

What happened:

In console, I got this response

"message": "Call to undefined method Illuminate\Database\Query\Builder::getBoughtStock(\"{\"from\":\"01-2-2018\",\"to\":\"16-2-2018\"}\")()

What I've already tried to fix it:

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, Laravel, PHP, DB version:

Backpack: 3.3.8
Laravel: 5.5
PHP: 7.1.7
Mysql: 5.7.19

Ask-It-On-Stack-Overflow

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings