Govind Samrow
Govind Samrow

Reputation: 10187

DB::transaction throw Undefined variable Error Exception

Working on Laravel 5 DB::transaction but throwing following error:

exception 'ErrorException' with message 'Undefined variable: tasks'

Here is my code:

private function addTasks($tasks, $id) {
  DB::transaction(function() {
    DB::table('task')->whereIn('id', $tasks)->update(array('task_parent_id' => $id));
  });
}

Note: Its working fine without DB::transaction function.

I've checked every line of code but found nothing wrong, please let me know where did wrong?

Upvotes: 2

Views: 2201

Answers (2)

Andriy Lozynskiy
Andriy Lozynskiy

Reputation: 2604

You should pass variable $tasks to anonymous function:
Try this

private function addTasks($tasks, $id) {
      DB::transaction(function() use ($tasks, $id) {
        DB::table('task')->whereIn('id', $tasks)->update(array('task_parent_id' => $id));
      });
    }

Upvotes: 9

Murwa
Murwa

Reputation: 2278

Use this:

private function addTasks($tasks, $id) {
    DB::transaction(function() use ($tasks, $id) {
        DB::table('task')->whereIn('id',$tasks)->update(array('task_parent_id' => $id));
    });
}

Upvotes: 3

Related Questions