Laravel-datatables: How to add custom variable inside editColumn

Created on 15 Jul 2015  路  4Comments  路  Source: yajra/laravel-datatables

Hi,

Could anyone shed some light on how to add a variable inside the editColumn Method.

$taxRate = $company_detail->tax_rate;

$datatables = Datatables::of($items)
->editColumn('rrp_price', function($item) {
// return $item->rrp_price;
return round($item->rrp_price / ( 1 + ($taxRate / 100.0) ), 2);
})
return $datatables->make(true);

It is always showing me undefined variable $taxRate.

Most helpful comment

You should use use in your closure like below:

$taxRate = $company_detail->tax_rate;

$datatables = Datatables::of($items)
  ->editColumn('rrp_price', function($item) use($taxRate) {
  // return $item->rrp_price;
  return round($item->rrp_price / ( 1 + ($taxRate / 100.0) ), 2);
})
return $datatables->make(true);

All 4 comments

You should use use in your closure like below:

$taxRate = $company_detail->tax_rate;

$datatables = Datatables::of($items)
  ->editColumn('rrp_price', function($item) use($taxRate) {
  // return $item->rrp_price;
  return round($item->rrp_price / ( 1 + ($taxRate / 100.0) ), 2);
})
return $datatables->make(true);

Can i use multiple closure?

E.g ->editColumn('rrp_price', function($item) use($taxRate, $newVar) {

or should I just make it into an array?

Yes, you can pass multiple variables or in array. It depends on your preference but I suggest you pass each variable.

Okay, noted and thank you for your quick response. It's working now :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mantrax314 picture mantrax314  路  15Comments

MahdiPishguy picture MahdiPishguy  路  17Comments

faisalhilmi picture faisalhilmi  路  18Comments

AbuHamdah picture AbuHamdah  路  33Comments

FaZeRs picture FaZeRs  路  18Comments