I want to call query before yajra code begins in controller and use it in custom column of yajra code like this
//Query before code
$users = user::get();
if ($request->ajax() && $request->draw) {
$data = table_name::get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('user', function($row){
foreach($users as $item){
//USE CODE DATA HERE
}
return $data;
})
->rawColumns(['user'])
->make(true);
}
Like this it don't recognize $users variable. How can I achieve this?
To access the
$usersvariable, you can use theusekeyword to import the variable.If you want to learn more about using inherited variables from the parent scope, you can refer to the documentation here: Anonymous Functions - PHP Manual Example #3