1

I as using DataTables in Laravel 5.1 and I want to increment a variable in a function but this is the error.

Undefined variable: index

public function create() { $index = 0; return Datatables::of(news::select('news.id as check', 'news.id as number', 'news.title', 'news.body', 'news.hits', 'news.created_at')) ->editColumn('check', function ($row) { return '<input type="checkbox" name="checkedBox1[]" value="' . $row->check . '">'; }) ->editColumn('number', function ($row) { return ++$index; }) ->editColumn('created_at', function ($row) { return jDate::forge(strtotime($row->created_at))->format('datetime'); }) ->make(); } 

1 Answer 1

1

You need

 ->editColumn('number', function($row) use (&$index){ return ++$index; }) 

Instead of

 ->editColumn('number', function($row){ return ++$index; }) 

The use keyword is to pass closures variables from scope and the & makes it able to edit the original variable

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.