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

FilipeBorges1993 picture FilipeBorges1993  ·  3Comments

jgatringer picture jgatringer  ·  3Comments

sangnguyenplus picture sangnguyenplus  ·  3Comments

t0n1zz picture t0n1zz  ·  3Comments

hohuuhau picture hohuuhau  ·  3Comments