Good day, please I have something that I can't catch the error, I am trying to implement DB::transaction() in my project, I have 3 tables I have to insert data, I created my own function to do it before, but I want to refactor my code to use laravel DB::transaction(). I went through the documentation Laravel docs, but I am having an error, the error is that it is not seeing the variables that I am passing inside the function in the transaction
'introduction' => $request->introduction,
I am getting **Undefined variable 'request' Below is my complete function
public function store(Request $request)
{
$data = $request->all();
$validator = Validator::make($data, [
'name' => 'required|max:255',
'introduction' => 'required|max:255',
'budget' => 'required'
]);
if ($validator->fails()) {
return response(['error' => $validator->errors(), 'Validation Error']);
}
$id = Auth::id();
// $project = Project::create($data);
DB::transaction(function () {
$project = Project::create([
'name' => $request->name,
'introduction' => $request->introduction,
'budget' => $request->budget
]);
DB::table('approvals')->insert(['project_id' => $project->id]);
DB::table('agreements')->insert(['project_id' => $project->id]);
});
}
How do I pass the $request->name to the function, thank you in advance