Deepesh Thapa
Deepesh Thapa

Reputation: 1789

How to pass two parameters in datatable's closure [Serverside]

I need to run a function to check for validity, therefore, I need to pass two variables on closure function. If possible I need to know if i can use global variable inside Datatables.

Example I want to achieve:

return Datatables::of($patients)
        ->addColumn('title', function ($patients,$value) {  
               -- *other codes here*
        })->make(true);

Example 2 if above solution cannot be achieved.

$value="something";    
return Datatables::of($patients)
        ->addColumn('title', function ($patients) {  
               -- use $value here
        })->make(true);

Now I need both $patients and $value to check inside addColumn. If i use the fucntion as above it throws an error [the above code doesnot work with two parameters]. If I try to use a $value from outside then datatable returns undefined variable $value.

I need solution which could either work as below:

This is a server side Laravel datatables using yajra datatbles package.

Upvotes: 0

Views: 2466

Answers (1)

Faizal Triswanto
Faizal Triswanto

Reputation: 13

try this

    //if singgle object

$value="something";    
return Datatables::of($patients)
        ->addColumn('title', function ($patients) use ($value) {  
               return $value;
        })
        ->rawColumns(['tittle'])
        ->make(true);

// if multiple / array like mysql

$value = // DB
return Datatables::of($patients)
        ->addColumn('title', function ($patients) use ($value) {  
               return json_decode($value)[0]->atributes_table;// 0 is array
        })
        ->rawColumns(['tittle'])
        ->make(true);

Upvotes: 1

Related Questions