Reputation: 10187
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
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
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